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];
37 void WriteOpers(char* text, ...);
39 void Exit (int status)
43 send_error("Server shutdown.");
45 // close down all listening sockets
46 for (int count = 0; count < boundPortCount; count++)
48 shutdown(openSockfd[count], 2);
54 void Killed(int status)
58 send_error("Server terminated.");
59 // close down all listening sockets
60 for (int count = 0; count < boundPortCount; count++)
62 shutdown(openSockfd[count], 2);
67 void Rehash(int status)
69 WriteOpers("Rehashing config file %s due to SIGHUP",CONFIG_FILE);
70 ReadConfig(false,NULL);
77 printf("\033[1mInspire Internet Relay Chat Server, compiled " __DATE__ " at " __TIME__ "\n");
78 printf("(C) ChatSpike Development team.\033[0;37m\n\n");
79 printf("Developers:\033[1m Brain, FrostyCoolSlug\n");
80 printf("Documentation:\033[1m FrostyCoolSlug, w00t\n");
81 printf("Testers:\033[1m typobox43, piggles, Lord_Zathras, CC\n");
82 printf("Name concept:\033[1m Lord_Zathras\n\n");
85 void WritePID(std::string filename)
87 ofstream outfile(filename.c_str());
88 if (outfile.is_open())
95 printf("Failed to write PID-file '%s', exiting.\n",filename.c_str());
96 log(DEFAULT,"Failed to write PID-file '%s', exiting.",filename.c_str());
101 void DeadPipe(int status)
103 signal (SIGPIPE, DeadPipe);
106 int DaemonSeed (void)
109 signal (SIGALRM, SIG_IGN);
110 signal (SIGHUP, Rehash);
111 signal (SIGPIPE, DeadPipe);
112 signal (SIGTERM, Exit);
113 signal (SIGABRT, Exit);
114 signal (SIGSEGV, Error);
115 signal (SIGURG, Exit);
116 signal (SIGKILL, Exit);
117 if ((childpid = fork ()) < 0)
119 else if (childpid > 0)
123 /* close stdin, stdout, stderr */
124 freopen("/dev/null","w",stdout);
125 freopen("/dev/null","w",stderr);
127 setpriority(PRIO_PROCESS,(int)getpid(),15); /* ircd sets to low process priority so it doesnt hog the box */
130 if (getrlimit(RLIMIT_CORE, &rl) == -1)
132 log(DEFAULT,"Failed to getrlimit()!");
137 rl.rlim_cur = rl.rlim_max;
138 if (setrlimit(RLIMIT_CORE, &rl) == -1)
139 log(DEFAULT,"setrlimit() failed, cannot increase coredump size.");
146 /* Make Sure Modules Are Avaliable!
147 * (BugFix By Craig.. See? I do work! :p)
148 * Modified by brain, requires const char*
149 * to work with other API functions
152 bool FileExists (const char* file)
155 if ((input = fopen (file, "r")) == NULL)
166 /* ConfProcess does the following things to a config line in the following order:
168 * Processes the line for syntax errors as shown below
169 * (1) Line void of quotes or equals (a malformed, illegal tag format)
170 * (2) Odd number of quotes on the line indicating a missing quote
171 * (3) number of equals signs not equal to number of quotes / 2 (missing an equals sign)
172 * (4) Spaces between the opening bracket (<) and the keyword
173 * (5) Spaces between a keyword and an equals sign
174 * (6) Spaces between an equals sign and a quote
175 * Removes trailing spaces
176 * Removes leading spaces
177 * Converts tabs to spaces
178 * Turns multiple spaces that are outside of quotes into single spaces
181 std::string ConfProcess(char* buffer, long linenumber, std::stringstream* errorstream, bool &error, std::string filename)
183 long number_of_quotes = 0;
184 long number_of_equals = 0;
185 bool has_open_bracket = false;
186 bool in_quotes = false;
192 // firstly clean up the line by stripping spaces from the start and end and converting tabs to spaces
193 for (int d = 0; d < strlen(buffer); d++)
194 if ((buffer[d]) == 9)
196 while ((buffer[0] == ' ') && (strlen(buffer)>0)) buffer++;
197 while ((buffer[strlen(buffer)-1] == ' ') && (strlen(buffer)>0)) buffer[strlen(buffer)-1] = '\0';
198 // empty lines are syntactically valid
199 if (!strcmp(buffer,""))
201 else if (buffer[0] == '#')
203 for (int c = 0; c < strlen(buffer); c++)
205 // convert all spaces that are OUTSIDE quotes into hardspace (0xA0) as this will make them easier to
206 // search and replace later :)
207 if ((!in_quotes) && (buffer[c] == ' '))
209 if ((buffer[c] == '<') && (!in_quotes))
211 has_open_bracket = true;
212 if (strlen(buffer) == 1)
214 *errorstream << "Tag without identifier at " << filename << ":" << linenumber << endl;
218 else if ((tolower(buffer[c+1]) < 'a') || (tolower(buffer[c+1]) > 'z'))
220 *errorstream << "Invalid characters in identifier at " << filename << ":" << linenumber << endl;
225 if (buffer[c] == '"')
228 in_quotes = (!in_quotes);
230 if ((buffer[c] == '=') && (!in_quotes))
233 if (strlen(buffer) == c)
235 *errorstream << "Variable without a value at " << filename << ":" << linenumber << endl;
239 else if (buffer[c+1] != '"')
241 *errorstream << "Variable name not followed immediately by its value at " << filename << ":" << linenumber << endl;
247 *errorstream << "Value without a variable (line starts with '=') at " << filename << ":" << linenumber << endl;
251 else if (buffer[c-1] == '\xA0')
253 *errorstream << "Variable name not followed immediately by its value at " << filename << ":" << linenumber << endl;
259 // no quotes, and no equals. something freaky.
260 if ((!number_of_quotes) || (!number_of_equals) && (strlen(buffer)>2) && (buffer[0]=='<'))
262 *errorstream << "Malformed tag at " << filename << ":" << linenumber << endl;
266 // odd number of quotes. thats just wrong.
267 if ((number_of_quotes % 2) != 0)
269 *errorstream << "Missing \" at " << filename << ":" << linenumber << endl;
273 if (number_of_equals < (number_of_quotes/2))
275 *errorstream << "Missing '=' at " << filename << ":" << linenumber << endl;
277 if (number_of_equals > (number_of_quotes/2))
279 *errorstream << "Too many '=' at " << filename << ":" << linenumber << endl;
282 std::string parsedata = buffer;
283 // turn multispace into single space
284 while (parsedata.find("\xA0\xA0") != std::string::npos)
286 parsedata.erase(parsedata.find("\xA0\xA0"),1);
289 // turn our hardspace back into softspace
290 for (int d = 0; d < parsedata.length(); d++)
292 if (parsedata[d] == '\xA0')
296 // and we're done, the line is fine!
300 bool LoadConf(const char* filename, std::stringstream *target, std::stringstream* errorstream)
303 errorstream->str("");
305 // first, check that the file exists before we try to do anything with it
306 if (!FileExists(filename))
308 *errorstream << "File " << filename << " not found." << endl;
311 // Fix the chmod of the file to restrict it to the current user and group
312 chmod(filename,0600);
314 FILE* conf = fopen(filename,"r");
320 if (fgets(buffer, MAXBUF, conf))
322 if ((!feof(conf)) && (buffer) && (strlen(buffer)))
324 if ((buffer[0] != '#') && (buffer[0] != '\r') && (buffer[0] != '\n'))
327 std::string data = ConfProcess(buffer,linenumber++,errorstream,error,filename);
344 /* Counts the number of tags of a certain type within the config file, e.g. to enumerate opers */
346 int EnumConf(std::stringstream *config, const char* tag)
349 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
350 int in_token, in_quotes, tptr, j, idx = 0;
353 const char* buf = config->str().c_str();
355 long len = strlen(buf);
365 if ((c == '#') && (lastc == '\n'))
367 while ((c != '\n') && (bptr<len))
373 if ((c == '<') && (!in_quotes))
388 in_quotes = (!in_quotes);
390 if ((c == '>') && (!in_quotes))
393 if (!strcmp(c_tag,tag))
395 /* correct tag, but wrong index */
405 if ((in_token) && (c != '\n') && (c != '\r'))
415 /* Counts the number of values within a certain tag */
417 int EnumValues(std::stringstream *config, const char* tag, int index)
420 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
421 int in_token, in_quotes, tptr, j, idx = 0;
424 bool correct_tag = false;
427 const char* buf = config->str().c_str();
429 long len = strlen(buf);
439 if ((c == '#') && (lastc == '\n'))
441 while ((c != '\n') && (bptr<len))
447 if ((c == '<') && (!in_quotes))
458 if ((!strcmp(c_tag,tag)) && (idx == index))
467 in_quotes = (!in_quotes);
470 if ( (correct_tag) && (!in_quotes) && ( (c == ' ') || (c == '\n') || (c == '\r') ) )
474 if ((c == '>') && (!in_quotes))
479 if (!strcmp(c_tag,tag))
481 /* correct tag, but wrong index */
491 if ((in_token) && (c != '\n') && (c != '\r'))
503 int ConfValueEnum(char* tag, std::stringstream* config)
505 return EnumConf(config,tag);
510 /* Retrieves a value from the config file. If there is more than one value of the specified
511 * key and section (e.g. for opers etc) then the index value specifies which to retreive, e.g.
513 * ConfValue("oper","name",2,result);
516 int ReadConf(std::stringstream *config, const char* tag, const char* var, int index, char *result)
519 char buffer[65535], c_tag[MAXBUF], c, lastc;
520 int in_token, in_quotes, tptr, j, idx = 0;
523 const char* buf = config->str().c_str();
525 long len = strlen(buf);
537 // FIX: Treat tabs as spaces
540 if ((c == '<') && (!in_quotes))
551 // FIX: Tab can follow a tagname as well as space.
552 } while ((c != ' ') && (c != 9));
556 in_quotes = (!in_quotes);
558 if ((c == '>') && (!in_quotes))
563 if (!strcmp(c_tag,tag))
565 if ((buffer) && (c_tag) && (var))
567 key = strstr(buffer,var);
570 /* value not found in tag */
588 for (j = 0; j < strlen(key); j++)
595 strlcpy(result,key,MAXBUF);
601 if (!strcmp(c_tag,tag))
603 /* correct tag, but wrong index */
613 if ((in_token) && (c != '\n') && (c != '\r'))
620 strcpy(result,""); // value or its tag not found at all
626 int ConfValue(char* tag, char* var, int index, char *result,std::stringstream *config)
628 ReadConf(config, tag, var, index, result);
634 // This will bind a socket to a port. It works for UDP/TCP
635 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
637 bzero((char *)&server,sizeof(server));
639 inet_aton(addr,&addy);
640 server.sin_family = AF_INET;
641 if (!strcmp(addr,""))
643 server.sin_addr.s_addr = htonl(INADDR_ANY);
647 server.sin_addr = addy;
649 server.sin_port = htons(port);
650 if (bind(sockfd,(struct sockaddr*)&server,sizeof(server))<0)
663 int OpenTCPSocket (void)
667 struct linger linger = { 0 };
669 if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
673 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on));
674 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
677 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (const char*)&linger,sizeof(linger));