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 void WriteOpers(char* text, ...);
38 void Exit (int status)
42 send_error("Server shutdown.");
44 // close down all listening sockets
45 for (int count = 0; count < boundPortCount; count++)
47 shutdown(openSockfd[count], 2);
53 void Killed(int status)
57 send_error("Server terminated.");
61 void Rehash(int status)
63 WriteOpers("Rehashing config file %s due to SIGHUP",CONFIG_FILE);
64 ReadConfig(false,NULL);
71 printf("\033[1;37mInspire Internet Relay Chat Server, compiled " __DATE__ " at " __TIME__ "\n");
72 printf("(C) ChatSpike Development team.\033[0;37m\n\n");
73 printf("\033[1;37mDevelopers:\033[0;37m Brain, FrostyCoolSlug\n");
74 printf("\033[1;37mDocumentation:\033[0;37m FrostyCoolSlug, w00t\n");
75 printf("\033[1;37mTesters:\033[0;37m typobox43, piggles, Lord_Zathras, CC\n");
76 printf("\033[1;37mName concept:\033[0;37m Lord_Zathras\n\n");
79 void WritePID(std::string filename)
81 ofstream outfile(filename.c_str());
82 if (outfile.is_open())
89 printf("Failed to write PID-file '%s', exiting.\n",filename.c_str());
94 void DeadPipe(int status)
96 signal (SIGPIPE, DeadPipe);
102 signal (SIGALRM, SIG_IGN);
103 signal (SIGHUP, Rehash);
104 signal (SIGPIPE, DeadPipe);
105 signal (SIGTERM, Exit);
106 signal (SIGABRT, Exit);
107 signal (SIGSEGV, Error);
108 signal (SIGURG, Exit);
109 signal (SIGKILL, Exit);
110 if ((childpid = fork ()) < 0)
112 else if (childpid > 0)
116 /* close stdout, stdin, stderr */
121 setpriority(PRIO_PROCESS,(int)getpid(),15); /* ircd sets to low process priority so it doesnt hog the box */
127 /* Make Sure Modules Are Avaliable!
128 * (BugFix By Craig.. See? I do work! :p)
129 * Modified by brain, requires const char*
130 * to work with other API functions
133 bool FileExists (const char* file)
136 if ((input = fopen (file, "r")) == NULL)
147 /* ConfProcess does the following things to a config line in the following order:
149 * Processes the line for syntax errors as shown below
150 * (1) Line void of quotes or equals (a malformed, illegal tag format)
151 * (2) Odd number of quotes on the line indicating a missing quote
152 * (3) number of equals signs not equal to number of quotes / 2 (missing an equals sign)
153 * (4) Spaces between the opening bracket (<) and the keyword
154 * (5) Spaces between a keyword and an equals sign
155 * (6) Spaces between an equals sign and a quote
156 * Removes trailing spaces
157 * Removes leading spaces
158 * Converts tabs to spaces
159 * Turns multiple spaces that are outside of quotes into single spaces
162 std::string ConfProcess(char* buffer, long linenumber, std::stringstream* errorstream, bool &error, std::string filename)
164 long number_of_quotes = 0;
165 long number_of_equals = 0;
166 bool has_open_bracket = false;
167 bool in_quotes = false;
173 // firstly clean up the line by stripping spaces from the start and end and converting tabs to spaces
174 for (int d = 0; d < strlen(buffer); d++)
175 if ((buffer[d]) == 9)
177 while ((buffer[0] == ' ') && (strlen(buffer)>0)) buffer++;
178 while ((buffer[strlen(buffer)-1] == ' ') && (strlen(buffer)>0)) buffer[strlen(buffer)-1] = '\0';
179 // empty lines are syntactically valid
180 if (!strcmp(buffer,""))
182 else if (buffer[0] == '#')
184 for (int c = 0; c < strlen(buffer); c++)
186 // convert all spaces that are OUTSIDE quotes into hardspace (0xA0) as this will make them easier to
187 // search and replace later :)
188 if ((!in_quotes) && (buffer[c] == ' '))
190 if ((buffer[c] == '<') && (!in_quotes))
192 has_open_bracket = true;
193 if (strlen(buffer) == 1)
195 *errorstream << "Tag without identifier at " << filename << ":" << linenumber << endl;
199 else if ((tolower(buffer[c+1]) < 'a') || (tolower(buffer[c+1]) > 'z'))
201 *errorstream << "Invalid characters in identifier at " << filename << ":" << linenumber << endl;
206 if (buffer[c] == '"')
209 in_quotes = (!in_quotes);
211 if ((buffer[c] == '=') && (!in_quotes))
214 if (strlen(buffer) == c)
216 *errorstream << "Variable without a value at " << filename << ":" << linenumber << endl;
220 else if (buffer[c+1] != '"')
222 *errorstream << "Variable name not followed immediately by its value at " << filename << ":" << linenumber << endl;
228 *errorstream << "Value without a variable (line starts with '=') at " << filename << ":" << linenumber << endl;
232 else if (buffer[c-1] == '\xA0')
234 *errorstream << "Variable name not followed immediately by its value at " << filename << ":" << linenumber << endl;
240 // no quotes, and no equals. something freaky.
241 if ((!number_of_quotes) || (!number_of_equals) && (strlen(buffer)>2) && (buffer[0]=='<'))
243 *errorstream << "Malformed tag at " << filename << ":" << linenumber << endl;
247 // odd number of quotes. thats just wrong.
248 if ((number_of_quotes % 2) != 0)
250 *errorstream << "Missing \" at " << filename << ":" << linenumber << endl;
254 if (number_of_equals < (number_of_quotes/2))
256 *errorstream << "Missing '=' at " << filename << ":" << linenumber << endl;
258 if (number_of_equals > (number_of_quotes/2))
260 *errorstream << "Too many '=' at " << filename << ":" << linenumber << endl;
263 std::string parsedata = buffer;
264 // turn multispace into single space
265 while (parsedata.find("\xA0\xA0") != std::string::npos)
267 parsedata.erase(parsedata.find("\xA0\xA0"),1);
270 // turn our hardspace back into softspace
271 for (int d = 0; d < parsedata.length(); d++)
273 if (parsedata[d] == '\xA0')
277 // and we're done, the line is fine!
281 bool LoadConf(const char* filename, std::stringstream *target, std::stringstream* errorstream)
284 errorstream->str("");
286 // first, check that the file exists before we try to do anything with it
287 if (!FileExists(filename))
289 *errorstream << "File " << filename << " not found." << endl;
292 // Fix the chmod of the file to restrict it to the current user and group
293 chmod(filename,0600);
295 FILE* conf = fopen(filename,"r");
301 if (fgets(buffer, MAXBUF, conf))
303 if ((!feof(conf)) && (buffer) && (strlen(buffer)))
305 if ((buffer[0] != '#') && (buffer[0] != '\r') && (buffer[0] != '\n'))
308 std::string data = ConfProcess(buffer,linenumber++,errorstream,error,filename);
325 /* Counts the number of tags of a certain type within the config file, e.g. to enumerate opers */
327 int EnumConf(std::stringstream *config, const char* tag)
330 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
331 int in_token, in_quotes, tptr, j, idx = 0;
334 const char* buf = config->str().c_str();
336 long len = strlen(buf);
346 if ((c == '#') && (lastc == '\n'))
348 while ((c != '\n') && (bptr<len))
354 if ((c == '<') && (!in_quotes))
369 in_quotes = (!in_quotes);
371 if ((c == '>') && (!in_quotes))
374 if (!strcmp(c_tag,tag))
376 /* correct tag, but wrong index */
386 if ((in_token) && (c != '\n') && (c != '\r'))
396 /* Counts the number of values within a certain tag */
398 int EnumValues(std::stringstream *config, const char* tag, int index)
401 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
402 int in_token, in_quotes, tptr, j, idx = 0;
405 bool correct_tag = false;
408 const char* buf = config->str().c_str();
410 long len = strlen(buf);
420 if ((c == '#') && (lastc == '\n'))
422 while ((c != '\n') && (bptr<len))
428 if ((c == '<') && (!in_quotes))
439 if ((!strcmp(c_tag,tag)) && (idx == index))
448 in_quotes = (!in_quotes);
451 if ( (correct_tag) && (!in_quotes) && ( (c == ' ') || (c == '\n') || (c == '\r') ) )
455 if ((c == '>') && (!in_quotes))
460 if (!strcmp(c_tag,tag))
462 /* correct tag, but wrong index */
472 if ((in_token) && (c != '\n') && (c != '\r'))
484 int ConfValueEnum(char* tag, std::stringstream* config)
486 return EnumConf(config,tag);
491 /* Retrieves a value from the config file. If there is more than one value of the specified
492 * key and section (e.g. for opers etc) then the index value specifies which to retreive, e.g.
494 * ConfValue("oper","name",2,result);
497 int ReadConf(std::stringstream *config, const char* tag, const char* var, int index, char *result)
500 char buffer[65535], c_tag[MAXBUF], c, lastc;
501 int in_token, in_quotes, tptr, j, idx = 0;
504 const char* buf = config->str().c_str();
506 long len = strlen(buf);
518 // FIX: Treat tabs as spaces
521 if ((c == '<') && (!in_quotes))
532 // FIX: Tab can follow a tagname as well as space.
533 } while ((c != ' ') && (c != 9));
537 in_quotes = (!in_quotes);
539 if ((c == '>') && (!in_quotes))
544 if (!strcmp(c_tag,tag))
546 if ((buffer) && (c_tag) && (var))
548 key = strstr(buffer,var);
551 /* value not found in tag */
569 for (j = 0; j < strlen(key); j++)
576 strlcpy(result,key,MAXBUF);
582 if (!strcmp(c_tag,tag))
584 /* correct tag, but wrong index */
594 if ((in_token) && (c != '\n') && (c != '\r'))
601 strcpy(result,""); // value or its tag not found at all
607 int ConfValue(char* tag, char* var, int index, char *result,std::stringstream *config)
609 ReadConf(config, tag, var, index, result);
615 // This will bind a socket to a port. It works for UDP/TCP
616 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
618 bzero((char *)&server,sizeof(server));
620 inet_aton(addr,&addy);
621 server.sin_family = AF_INET;
622 if (!strcmp(addr,""))
624 server.sin_addr.s_addr = htonl(INADDR_ANY);
628 server.sin_addr = addy;
630 server.sin_port = htons(port);
631 if (bind(sockfd,(struct sockaddr*)&server,sizeof(server))<0)
644 int OpenTCPSocket (void)
648 struct linger linger = { 0 };
650 if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
654 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on));
655 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
658 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (const char*)&linger,sizeof(linger));