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;
34 void WriteOpers(char* text, ...);
36 void Exit (int status)
40 send_error("Server shutdown.");
44 void Killed(int status)
48 send_error("Server terminated.");
52 void Rehash(int status)
54 WriteOpers("Rehashing config file %s due to SIGHUP",CONFIG_FILE);
55 ReadConfig(false,NULL);
62 printf("\033[1;37mInspire Internet Relay Chat Server, compiled " __DATE__ " at " __TIME__ "\n");
63 printf("(C) ChatSpike Development team.\033[0;37m\n\n");
64 printf("\033[1;37mDevelopers:\033[0;37m Brain, FrostyCoolSlug\n");
65 printf("\033[1;37mDocumentation:\033[0;37m FrostyCoolSlug, w00t\n");
66 printf("\033[1;37mTesters:\033[0;37m typobox43, piggles, Lord_Zathras, CC\n");
67 printf("\033[1;37mName concept:\033[0;37m Lord_Zathras\n\n");
70 void WritePID(std::string filename)
72 ofstream outfile(filename.c_str());
73 if (outfile.is_open())
80 printf("Failed to write PID-file '%s', exiting.\n",filename.c_str());
85 void DeadPipe(int status)
87 signal (SIGPIPE, DeadPipe);
93 signal (SIGALRM, SIG_IGN);
94 signal (SIGHUP, Rehash);
95 signal (SIGPIPE, DeadPipe);
96 signal (SIGTERM, Exit);
97 signal (SIGABRT, Exit);
98 signal (SIGSEGV, Error);
99 signal (SIGURG, Exit);
100 signal (SIGKILL, Exit);
101 if ((childpid = fork ()) < 0)
103 else if (childpid > 0)
107 /* close stdout, stdin, stderr */
112 setpriority(PRIO_PROCESS,(int)getpid(),15); /* ircd sets to low process priority so it doesnt hog the box */
118 /* Make Sure Modules Are Avaliable!
119 * (BugFix By Craig.. See? I do work! :p)
120 * Modified by brain, requires const char*
121 * to work with other API functions
124 bool FileExists (const char* file)
128 if ((input = fopen (file, "r")) == NULL) { return(false); }
129 else { fclose (input); return(true); }
132 /* ConfProcess does the following things to a config line in the following order:
134 * Processes the line for syntax errors as shown below
135 * (1) Line void of quotes or equals (a malformed, illegal tag format)
136 * (2) Odd number of quotes on the line indicating a missing quote
137 * (3) number of equals signs not equal to number of quotes / 2 (missing an equals sign)
138 * (4) Spaces between the opening bracket (<) and the keyword
139 * (5) Spaces between a keyword and an equals sign
140 * (6) Spaces between an equals sign and a quote
141 * Removes trailing spaces
142 * Removes leading spaces
143 * Converts tabs to spaces
144 * Turns multiple spaces that are outside of quotes into single spaces
147 std::string ConfProcess(char* buffer, long linenumber, std::stringstream* errorstream, bool &error, std::string filename)
149 long number_of_quotes = 0;
150 long number_of_equals = 0;
151 bool has_open_bracket = false;
152 bool in_quotes = false;
158 // firstly clean up the line by stripping spaces from the start and end and converting tabs to spaces
159 for (int d = 0; d < strlen(buffer); d++)
160 if ((buffer[d]) == 9)
162 while ((buffer[0] == ' ') && (strlen(buffer)>0)) buffer++;
163 while ((buffer[strlen(buffer)-1] == ' ') && (strlen(buffer)>0)) buffer[strlen(buffer)-1] = '\0';
164 // empty lines are syntactically valid
165 if (!strcmp(buffer,""))
167 else if (buffer[0] == '#')
169 for (int c = 0; c < strlen(buffer); c++)
171 // convert all spaces that are OUTSIDE quotes into hardspace (0xA0) as this will make them easier to
172 // search and replace later :)
173 if ((!in_quotes) && (buffer[c] == ' '))
175 if ((buffer[c] == '<') && (!in_quotes))
177 has_open_bracket = true;
178 if (strlen(buffer) == 1)
180 *errorstream << "Tag without identifier at " << filename << ":" << linenumber << endl;
184 else if ((tolower(buffer[c+1]) < 'a') || (tolower(buffer[c+1]) > 'z'))
186 *errorstream << "Invalid characters in identifier at " << filename << ":" << linenumber << endl;
191 if (buffer[c] == '"')
194 in_quotes = (!in_quotes);
196 if ((buffer[c] == '=') && (!in_quotes))
199 if (strlen(buffer) == c)
201 *errorstream << "Variable without a value at " << filename << ":" << linenumber << endl;
205 else if (buffer[c+1] != '"')
207 *errorstream << "Variable name not followed immediately by its value at " << filename << ":" << linenumber << endl;
213 *errorstream << "Value without a variable (line starts with '=') at " << filename << ":" << linenumber << endl;
217 else if (buffer[c-1] == '\xA0')
219 *errorstream << "Variable name not followed immediately by its value at " << filename << ":" << linenumber << endl;
225 // no quotes, and no equals. something freaky.
226 if ((!number_of_quotes) || (!number_of_equals) && (strlen(buffer)>2) && (buffer[0]=='<'))
228 *errorstream << "Malformed tag at " << filename << ":" << linenumber << endl;
232 // odd number of quotes. thats just wrong.
233 if ((number_of_quotes % 2) != 0)
235 *errorstream << "Missing \" at " << filename << ":" << linenumber << endl;
239 if (number_of_equals < (number_of_quotes/2))
241 *errorstream << "Missing '=' at " << filename << ":" << linenumber << endl;
243 if (number_of_equals > (number_of_quotes/2))
245 *errorstream << "Too many '=' at " << filename << ":" << linenumber << endl;
248 std::string parsedata = buffer;
249 // turn multispace into single space
250 while (parsedata.find("\xA0\xA0") != std::string::npos)
252 parsedata.erase(parsedata.find("\xA0\xA0"),1);
255 // turn our hardspace back into softspace
256 for (int d = 0; d < parsedata.length(); d++)
258 if (parsedata[d] == '\xA0')
262 // and we're done, the line is fine!
266 bool LoadConf(const char* filename, std::stringstream *target, std::stringstream* errorstream)
269 errorstream->str("");
271 FILE* conf = fopen(filename,"r");
272 if (!FileExists(filename))
274 *errorstream << "File " << filename << " not found." << endl;
282 if (fgets(buffer, MAXBUF, conf))
284 if ((!feof(conf)) && (buffer) && (strlen(buffer)))
286 if ((buffer[0] != '#') && (buffer[0] != '\r') && (buffer[0] != '\n'))
289 std::string data = ConfProcess(buffer,linenumber++,errorstream,error,filename);
306 /* Counts the number of tags of a certain type within the config file, e.g. to enumerate opers */
308 int EnumConf(std::stringstream *config, const char* tag)
311 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
312 int in_token, in_quotes, tptr, j, idx = 0;
315 const char* buf = config->str().c_str();
317 long len = strlen(buf);
327 if ((c == '#') && (lastc == '\n'))
329 while ((c != '\n') && (bptr<len))
335 if ((c == '<') && (!in_quotes))
350 in_quotes = (!in_quotes);
352 if ((c == '>') && (!in_quotes))
355 if (!strcmp(c_tag,tag))
357 /* correct tag, but wrong index */
367 if ((in_token) && (c != '\n') && (c != '\r'))
377 /* Counts the number of values within a certain tag */
379 int EnumValues(std::stringstream *config, const char* tag, int index)
382 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
383 int in_token, in_quotes, tptr, j, idx = 0;
386 bool correct_tag = false;
389 const char* buf = config->str().c_str();
391 long len = strlen(buf);
401 if ((c == '#') && (lastc == '\n'))
403 while ((c != '\n') && (bptr<len))
409 if ((c == '<') && (!in_quotes))
420 if ((!strcmp(c_tag,tag)) && (idx == index))
429 in_quotes = (!in_quotes);
432 if ( (correct_tag) && (!in_quotes) && ( (c == ' ') || (c == '\n') || (c == '\r') ) )
436 if ((c == '>') && (!in_quotes))
441 if (!strcmp(c_tag,tag))
443 /* correct tag, but wrong index */
453 if ((in_token) && (c != '\n') && (c != '\r'))
465 int ConfValueEnum(char* tag, std::stringstream* config)
467 return EnumConf(config,tag);
472 /* Retrieves a value from the config file. If there is more than one value of the specified
473 * key and section (e.g. for opers etc) then the index value specifies which to retreive, e.g.
475 * ConfValue("oper","name",2,result);
478 int ReadConf(std::stringstream *config, const char* tag, const char* var, int index, char *result)
481 char buffer[65535], c_tag[MAXBUF], c, lastc;
482 int in_token, in_quotes, tptr, j, idx = 0;
485 const char* buf = config->str().c_str();
487 long len = strlen(buf);
499 // FIX: Treat tabs as spaces
502 if ((c == '<') && (!in_quotes))
513 // FIX: Tab can follow a tagname as well as space.
514 } while ((c != ' ') && (c != 9));
518 in_quotes = (!in_quotes);
520 if ((c == '>') && (!in_quotes))
525 if (!strcmp(c_tag,tag))
527 if ((buffer) && (c_tag) && (var))
529 key = strstr(buffer,var);
532 /* value not found in tag */
550 for (j = 0; j < strlen(key); j++)
557 strlcpy(result,key,MAXBUF);
563 if (!strcmp(c_tag,tag))
565 /* correct tag, but wrong index */
575 if ((in_token) && (c != '\n') && (c != '\r'))
582 strcpy(result,""); // value or its tag not found at all
588 int ConfValue(char* tag, char* var, int index, char *result,std::stringstream *config)
590 ReadConf(config, tag, var, index, result);
596 /* This will bind a socket to a port. It works for UDP/TCP */
597 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
599 bzero((char *)&server,sizeof(server));
601 inet_aton(addr,&addy);
603 server.sin_family = AF_INET;
604 if (!strcmp(addr,""))
606 server.sin_addr.s_addr = htonl(INADDR_ANY);
610 server.sin_addr = addy;
613 server.sin_port = htons(port);
615 if (bind(sockfd,(struct sockaddr*)&server,sizeof(server))<0)
627 /* Open a TCP Socket */
628 int OpenTCPSocket (void)
632 struct linger linger = { 0 };
634 if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
638 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on));
639 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
642 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (const char*)&linger,sizeof(linger));