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[1;37mInspire Internet Relay Chat Server, compiled " __DATE__ " at " __TIME__ "\n");
78 printf("(C) ChatSpike Development team.\033[0;37m\n\n");
79 printf("\033[1;37mDevelopers:\033[0;37m Brain, FrostyCoolSlug\n");
80 printf("\033[1;37mDocumentation:\033[0;37m FrostyCoolSlug, w00t\n");
81 printf("\033[1;37mTesters:\033[0;37m typobox43, piggles, Lord_Zathras, CC\n");
82 printf("\033[1;37mName concept:\033[0;37m 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 */
133 /* Make Sure Modules Are Avaliable!
134 * (BugFix By Craig.. See? I do work! :p)
135 * Modified by brain, requires const char*
136 * to work with other API functions
139 bool FileExists (const char* file)
142 if ((input = fopen (file, "r")) == NULL)
153 /* ConfProcess does the following things to a config line in the following order:
155 * Processes the line for syntax errors as shown below
156 * (1) Line void of quotes or equals (a malformed, illegal tag format)
157 * (2) Odd number of quotes on the line indicating a missing quote
158 * (3) number of equals signs not equal to number of quotes / 2 (missing an equals sign)
159 * (4) Spaces between the opening bracket (<) and the keyword
160 * (5) Spaces between a keyword and an equals sign
161 * (6) Spaces between an equals sign and a quote
162 * Removes trailing spaces
163 * Removes leading spaces
164 * Converts tabs to spaces
165 * Turns multiple spaces that are outside of quotes into single spaces
168 std::string ConfProcess(char* buffer, long linenumber, std::stringstream* errorstream, bool &error, std::string filename)
170 long number_of_quotes = 0;
171 long number_of_equals = 0;
172 bool has_open_bracket = false;
173 bool in_quotes = false;
179 // firstly clean up the line by stripping spaces from the start and end and converting tabs to spaces
180 for (int d = 0; d < strlen(buffer); d++)
181 if ((buffer[d]) == 9)
183 while ((buffer[0] == ' ') && (strlen(buffer)>0)) buffer++;
184 while ((buffer[strlen(buffer)-1] == ' ') && (strlen(buffer)>0)) buffer[strlen(buffer)-1] = '\0';
185 // empty lines are syntactically valid
186 if (!strcmp(buffer,""))
188 else if (buffer[0] == '#')
190 for (int c = 0; c < strlen(buffer); c++)
192 // convert all spaces that are OUTSIDE quotes into hardspace (0xA0) as this will make them easier to
193 // search and replace later :)
194 if ((!in_quotes) && (buffer[c] == ' '))
196 if ((buffer[c] == '<') && (!in_quotes))
198 has_open_bracket = true;
199 if (strlen(buffer) == 1)
201 *errorstream << "Tag without identifier at " << filename << ":" << linenumber << endl;
205 else if ((tolower(buffer[c+1]) < 'a') || (tolower(buffer[c+1]) > 'z'))
207 *errorstream << "Invalid characters in identifier at " << filename << ":" << linenumber << endl;
212 if (buffer[c] == '"')
215 in_quotes = (!in_quotes);
217 if ((buffer[c] == '=') && (!in_quotes))
220 if (strlen(buffer) == c)
222 *errorstream << "Variable without a value at " << filename << ":" << linenumber << endl;
226 else if (buffer[c+1] != '"')
228 *errorstream << "Variable name not followed immediately by its value at " << filename << ":" << linenumber << endl;
234 *errorstream << "Value without a variable (line starts with '=') at " << filename << ":" << linenumber << endl;
238 else if (buffer[c-1] == '\xA0')
240 *errorstream << "Variable name not followed immediately by its value at " << filename << ":" << linenumber << endl;
246 // no quotes, and no equals. something freaky.
247 if ((!number_of_quotes) || (!number_of_equals) && (strlen(buffer)>2) && (buffer[0]=='<'))
249 *errorstream << "Malformed tag at " << filename << ":" << linenumber << endl;
253 // odd number of quotes. thats just wrong.
254 if ((number_of_quotes % 2) != 0)
256 *errorstream << "Missing \" at " << filename << ":" << linenumber << endl;
260 if (number_of_equals < (number_of_quotes/2))
262 *errorstream << "Missing '=' at " << filename << ":" << linenumber << endl;
264 if (number_of_equals > (number_of_quotes/2))
266 *errorstream << "Too many '=' at " << filename << ":" << linenumber << endl;
269 std::string parsedata = buffer;
270 // turn multispace into single space
271 while (parsedata.find("\xA0\xA0") != std::string::npos)
273 parsedata.erase(parsedata.find("\xA0\xA0"),1);
276 // turn our hardspace back into softspace
277 for (int d = 0; d < parsedata.length(); d++)
279 if (parsedata[d] == '\xA0')
283 // and we're done, the line is fine!
287 bool LoadConf(const char* filename, std::stringstream *target, std::stringstream* errorstream)
290 errorstream->str("");
292 // first, check that the file exists before we try to do anything with it
293 if (!FileExists(filename))
295 *errorstream << "File " << filename << " not found." << endl;
298 // Fix the chmod of the file to restrict it to the current user and group
299 chmod(filename,0600);
301 FILE* conf = fopen(filename,"r");
307 if (fgets(buffer, MAXBUF, conf))
309 if ((!feof(conf)) && (buffer) && (strlen(buffer)))
311 if ((buffer[0] != '#') && (buffer[0] != '\r') && (buffer[0] != '\n'))
314 std::string data = ConfProcess(buffer,linenumber++,errorstream,error,filename);
331 /* Counts the number of tags of a certain type within the config file, e.g. to enumerate opers */
333 int EnumConf(std::stringstream *config, const char* tag)
336 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
337 int in_token, in_quotes, tptr, j, idx = 0;
340 const char* buf = config->str().c_str();
342 long len = strlen(buf);
352 if ((c == '#') && (lastc == '\n'))
354 while ((c != '\n') && (bptr<len))
360 if ((c == '<') && (!in_quotes))
375 in_quotes = (!in_quotes);
377 if ((c == '>') && (!in_quotes))
380 if (!strcmp(c_tag,tag))
382 /* correct tag, but wrong index */
392 if ((in_token) && (c != '\n') && (c != '\r'))
402 /* Counts the number of values within a certain tag */
404 int EnumValues(std::stringstream *config, const char* tag, int index)
407 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
408 int in_token, in_quotes, tptr, j, idx = 0;
411 bool correct_tag = false;
414 const char* buf = config->str().c_str();
416 long len = strlen(buf);
426 if ((c == '#') && (lastc == '\n'))
428 while ((c != '\n') && (bptr<len))
434 if ((c == '<') && (!in_quotes))
445 if ((!strcmp(c_tag,tag)) && (idx == index))
454 in_quotes = (!in_quotes);
457 if ( (correct_tag) && (!in_quotes) && ( (c == ' ') || (c == '\n') || (c == '\r') ) )
461 if ((c == '>') && (!in_quotes))
466 if (!strcmp(c_tag,tag))
468 /* correct tag, but wrong index */
478 if ((in_token) && (c != '\n') && (c != '\r'))
490 int ConfValueEnum(char* tag, std::stringstream* config)
492 return EnumConf(config,tag);
497 /* Retrieves a value from the config file. If there is more than one value of the specified
498 * key and section (e.g. for opers etc) then the index value specifies which to retreive, e.g.
500 * ConfValue("oper","name",2,result);
503 int ReadConf(std::stringstream *config, const char* tag, const char* var, int index, char *result)
506 char buffer[65535], c_tag[MAXBUF], c, lastc;
507 int in_token, in_quotes, tptr, j, idx = 0;
510 const char* buf = config->str().c_str();
512 long len = strlen(buf);
524 // FIX: Treat tabs as spaces
527 if ((c == '<') && (!in_quotes))
538 // FIX: Tab can follow a tagname as well as space.
539 } while ((c != ' ') && (c != 9));
543 in_quotes = (!in_quotes);
545 if ((c == '>') && (!in_quotes))
550 if (!strcmp(c_tag,tag))
552 if ((buffer) && (c_tag) && (var))
554 key = strstr(buffer,var);
557 /* value not found in tag */
575 for (j = 0; j < strlen(key); j++)
582 strlcpy(result,key,MAXBUF);
588 if (!strcmp(c_tag,tag))
590 /* correct tag, but wrong index */
600 if ((in_token) && (c != '\n') && (c != '\r'))
607 strcpy(result,""); // value or its tag not found at all
613 int ConfValue(char* tag, char* var, int index, char *result,std::stringstream *config)
615 ReadConf(config, tag, var, index, result);
621 // This will bind a socket to a port. It works for UDP/TCP
622 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
624 bzero((char *)&server,sizeof(server));
626 inet_aton(addr,&addy);
627 server.sin_family = AF_INET;
628 if (!strcmp(addr,""))
630 server.sin_addr.s_addr = htonl(INADDR_ANY);
634 server.sin_addr = addy;
636 server.sin_port = htons(port);
637 if (bind(sockfd,(struct sockaddr*)&server,sizeof(server))<0)
650 int OpenTCPSocket (void)
654 struct linger linger = { 0 };
656 if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
660 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on));
661 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
664 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (const char*)&linger,sizeof(linger));