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.");
58 // close down all listening sockets
59 for (int count = 0; count < boundPortCount; count++)
61 shutdown(openSockfd[count], 2);
66 void Rehash(int status)
68 WriteOpers("Rehashing config file %s due to SIGHUP",CONFIG_FILE);
69 ReadConfig(false,NULL);
76 printf("\033[1;37mInspire Internet Relay Chat Server, compiled " __DATE__ " at " __TIME__ "\n");
77 printf("(C) ChatSpike Development team.\033[0;37m\n\n");
78 printf("\033[1;37mDevelopers:\033[0;37m Brain, FrostyCoolSlug\n");
79 printf("\033[1;37mDocumentation:\033[0;37m FrostyCoolSlug, w00t\n");
80 printf("\033[1;37mTesters:\033[0;37m typobox43, piggles, Lord_Zathras, CC\n");
81 printf("\033[1;37mName concept:\033[0;37m Lord_Zathras\n\n");
84 void WritePID(std::string filename)
86 ofstream outfile(filename.c_str());
87 if (outfile.is_open())
94 printf("Failed to write PID-file '%s', exiting.\n",filename.c_str());
99 void DeadPipe(int status)
101 signal (SIGPIPE, DeadPipe);
104 int DaemonSeed (void)
107 signal (SIGALRM, SIG_IGN);
108 signal (SIGHUP, Rehash);
109 signal (SIGPIPE, DeadPipe);
110 signal (SIGTERM, Exit);
111 signal (SIGABRT, Exit);
112 signal (SIGSEGV, Error);
113 signal (SIGURG, Exit);
114 signal (SIGKILL, Exit);
115 if ((childpid = fork ()) < 0)
117 else if (childpid > 0)
121 /* close stdout, stdin, stderr */
126 setpriority(PRIO_PROCESS,(int)getpid(),15); /* ircd sets to low process priority so it doesnt hog the box */
132 /* Make Sure Modules Are Avaliable!
133 * (BugFix By Craig.. See? I do work! :p)
134 * Modified by brain, requires const char*
135 * to work with other API functions
138 bool FileExists (const char* file)
141 if ((input = fopen (file, "r")) == NULL)
152 /* ConfProcess does the following things to a config line in the following order:
154 * Processes the line for syntax errors as shown below
155 * (1) Line void of quotes or equals (a malformed, illegal tag format)
156 * (2) Odd number of quotes on the line indicating a missing quote
157 * (3) number of equals signs not equal to number of quotes / 2 (missing an equals sign)
158 * (4) Spaces between the opening bracket (<) and the keyword
159 * (5) Spaces between a keyword and an equals sign
160 * (6) Spaces between an equals sign and a quote
161 * Removes trailing spaces
162 * Removes leading spaces
163 * Converts tabs to spaces
164 * Turns multiple spaces that are outside of quotes into single spaces
167 std::string ConfProcess(char* buffer, long linenumber, std::stringstream* errorstream, bool &error, std::string filename)
169 long number_of_quotes = 0;
170 long number_of_equals = 0;
171 bool has_open_bracket = false;
172 bool in_quotes = false;
178 // firstly clean up the line by stripping spaces from the start and end and converting tabs to spaces
179 for (int d = 0; d < strlen(buffer); d++)
180 if ((buffer[d]) == 9)
182 while ((buffer[0] == ' ') && (strlen(buffer)>0)) buffer++;
183 while ((buffer[strlen(buffer)-1] == ' ') && (strlen(buffer)>0)) buffer[strlen(buffer)-1] = '\0';
184 // empty lines are syntactically valid
185 if (!strcmp(buffer,""))
187 else if (buffer[0] == '#')
189 for (int c = 0; c < strlen(buffer); c++)
191 // convert all spaces that are OUTSIDE quotes into hardspace (0xA0) as this will make them easier to
192 // search and replace later :)
193 if ((!in_quotes) && (buffer[c] == ' '))
195 if ((buffer[c] == '<') && (!in_quotes))
197 has_open_bracket = true;
198 if (strlen(buffer) == 1)
200 *errorstream << "Tag without identifier at " << filename << ":" << linenumber << endl;
204 else if ((tolower(buffer[c+1]) < 'a') || (tolower(buffer[c+1]) > 'z'))
206 *errorstream << "Invalid characters in identifier at " << filename << ":" << linenumber << endl;
211 if (buffer[c] == '"')
214 in_quotes = (!in_quotes);
216 if ((buffer[c] == '=') && (!in_quotes))
219 if (strlen(buffer) == c)
221 *errorstream << "Variable without a value at " << filename << ":" << linenumber << endl;
225 else if (buffer[c+1] != '"')
227 *errorstream << "Variable name not followed immediately by its value at " << filename << ":" << linenumber << endl;
233 *errorstream << "Value without a variable (line starts with '=') at " << filename << ":" << linenumber << endl;
237 else if (buffer[c-1] == '\xA0')
239 *errorstream << "Variable name not followed immediately by its value at " << filename << ":" << linenumber << endl;
245 // no quotes, and no equals. something freaky.
246 if ((!number_of_quotes) || (!number_of_equals) && (strlen(buffer)>2) && (buffer[0]=='<'))
248 *errorstream << "Malformed tag at " << filename << ":" << linenumber << endl;
252 // odd number of quotes. thats just wrong.
253 if ((number_of_quotes % 2) != 0)
255 *errorstream << "Missing \" at " << filename << ":" << linenumber << endl;
259 if (number_of_equals < (number_of_quotes/2))
261 *errorstream << "Missing '=' at " << filename << ":" << linenumber << endl;
263 if (number_of_equals > (number_of_quotes/2))
265 *errorstream << "Too many '=' at " << filename << ":" << linenumber << endl;
268 std::string parsedata = buffer;
269 // turn multispace into single space
270 while (parsedata.find("\xA0\xA0") != std::string::npos)
272 parsedata.erase(parsedata.find("\xA0\xA0"),1);
275 // turn our hardspace back into softspace
276 for (int d = 0; d < parsedata.length(); d++)
278 if (parsedata[d] == '\xA0')
282 // and we're done, the line is fine!
286 bool LoadConf(const char* filename, std::stringstream *target, std::stringstream* errorstream)
289 errorstream->str("");
291 // first, check that the file exists before we try to do anything with it
292 if (!FileExists(filename))
294 *errorstream << "File " << filename << " not found." << endl;
297 // Fix the chmod of the file to restrict it to the current user and group
298 chmod(filename,0600);
300 FILE* conf = fopen(filename,"r");
306 if (fgets(buffer, MAXBUF, conf))
308 if ((!feof(conf)) && (buffer) && (strlen(buffer)))
310 if ((buffer[0] != '#') && (buffer[0] != '\r') && (buffer[0] != '\n'))
313 std::string data = ConfProcess(buffer,linenumber++,errorstream,error,filename);
330 /* Counts the number of tags of a certain type within the config file, e.g. to enumerate opers */
332 int EnumConf(std::stringstream *config, const char* tag)
335 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
336 int in_token, in_quotes, tptr, j, idx = 0;
339 const char* buf = config->str().c_str();
341 long len = strlen(buf);
351 if ((c == '#') && (lastc == '\n'))
353 while ((c != '\n') && (bptr<len))
359 if ((c == '<') && (!in_quotes))
374 in_quotes = (!in_quotes);
376 if ((c == '>') && (!in_quotes))
379 if (!strcmp(c_tag,tag))
381 /* correct tag, but wrong index */
391 if ((in_token) && (c != '\n') && (c != '\r'))
401 /* Counts the number of values within a certain tag */
403 int EnumValues(std::stringstream *config, const char* tag, int index)
406 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
407 int in_token, in_quotes, tptr, j, idx = 0;
410 bool correct_tag = false;
413 const char* buf = config->str().c_str();
415 long len = strlen(buf);
425 if ((c == '#') && (lastc == '\n'))
427 while ((c != '\n') && (bptr<len))
433 if ((c == '<') && (!in_quotes))
444 if ((!strcmp(c_tag,tag)) && (idx == index))
453 in_quotes = (!in_quotes);
456 if ( (correct_tag) && (!in_quotes) && ( (c == ' ') || (c == '\n') || (c == '\r') ) )
460 if ((c == '>') && (!in_quotes))
465 if (!strcmp(c_tag,tag))
467 /* correct tag, but wrong index */
477 if ((in_token) && (c != '\n') && (c != '\r'))
489 int ConfValueEnum(char* tag, std::stringstream* config)
491 return EnumConf(config,tag);
496 /* Retrieves a value from the config file. If there is more than one value of the specified
497 * key and section (e.g. for opers etc) then the index value specifies which to retreive, e.g.
499 * ConfValue("oper","name",2,result);
502 int ReadConf(std::stringstream *config, const char* tag, const char* var, int index, char *result)
505 char buffer[65535], c_tag[MAXBUF], c, lastc;
506 int in_token, in_quotes, tptr, j, idx = 0;
509 const char* buf = config->str().c_str();
511 long len = strlen(buf);
523 // FIX: Treat tabs as spaces
526 if ((c == '<') && (!in_quotes))
537 // FIX: Tab can follow a tagname as well as space.
538 } while ((c != ' ') && (c != 9));
542 in_quotes = (!in_quotes);
544 if ((c == '>') && (!in_quotes))
549 if (!strcmp(c_tag,tag))
551 if ((buffer) && (c_tag) && (var))
553 key = strstr(buffer,var);
556 /* value not found in tag */
574 for (j = 0; j < strlen(key); j++)
581 strlcpy(result,key,MAXBUF);
587 if (!strcmp(c_tag,tag))
589 /* correct tag, but wrong index */
599 if ((in_token) && (c != '\n') && (c != '\r'))
606 strcpy(result,""); // value or its tag not found at all
612 int ConfValue(char* tag, char* var, int index, char *result,std::stringstream *config)
614 ReadConf(config, tag, var, index, result);
620 // This will bind a socket to a port. It works for UDP/TCP
621 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
623 bzero((char *)&server,sizeof(server));
625 inet_aton(addr,&addy);
626 server.sin_family = AF_INET;
627 if (!strcmp(addr,""))
629 server.sin_addr.s_addr = htonl(INADDR_ANY);
633 server.sin_addr = addy;
635 server.sin_port = htons(port);
636 if (bind(sockfd,(struct sockaddr*)&server,sizeof(server))<0)
649 int OpenTCPSocket (void)
653 struct linger linger = { 0 };
655 if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
659 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on));
660 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
663 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (const char*)&linger,sizeof(linger));