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");
71 void DeadPipe(int status)
73 signal (SIGPIPE, DeadPipe);
79 signal (SIGALRM, SIG_IGN);
80 signal (SIGHUP, Rehash);
81 signal (SIGPIPE, DeadPipe);
82 signal (SIGTERM, Exit);
83 signal (SIGABRT, Exit);
84 signal (SIGSEGV, Error);
85 signal (SIGURG, Exit);
86 signal (SIGKILL, Exit);
87 if ((childpid = fork ()) < 0)
89 else if (childpid > 0)
93 /* close stdout, stdin, stderr */
98 setpriority(PRIO_PROCESS,(int)getpid(),15); /* ircd sets to low process priority so it doesnt hog the box */
104 /* Make Sure Modules Are Avaliable!
105 * (BugFix By Craig.. See? I do work! :p)
106 * Modified by brain, requires const char*
107 * to work with other API functions
110 bool FileExists (const char* file)
114 if ((input = fopen (file, "r")) == NULL) { return(false); }
115 else { fclose (input); return(true); }
118 /* ConfProcess does the following things to a config line in the following order:
120 * Processes the line for syntax errors as shown below
121 * (1) Line void of quotes or equals (a malformed, illegal tag format)
122 * (2) Odd number of quotes on the line indicating a missing quote
123 * (3) number of equals signs not equal to number of quotes / 2 (missing an equals sign)
124 * (4) Spaces between the opening bracket (<) and the keyword
125 * (5) Spaces between a keyword and an equals sign
126 * (6) Spaces between an equals sign and a quote
127 * Removes trailing spaces
128 * Removes leading spaces
129 * Converts tabs to spaces
130 * Turns multiple spaces that are outside of quotes into single spaces
133 std::string ConfProcess(char* buffer, long linenumber, std::stringstream* errorstream, bool &error, std::string filename)
135 long number_of_quotes = 0;
136 long number_of_equals = 0;
137 bool has_open_bracket = false;
138 bool in_quotes = false;
144 // firstly clean up the line by stripping spaces from the start and end and converting tabs to spaces
145 for (int d = 0; d < strlen(buffer); d++)
146 if ((buffer[d]) == 9)
148 while ((buffer[0] == ' ') && (strlen(buffer)>0)) buffer++;
149 while ((buffer[strlen(buffer)-1] == ' ') && (strlen(buffer)>0)) buffer[strlen(buffer)-1] = '\0';
150 // empty lines are syntactically valid
151 if (!strcmp(buffer,""))
153 else if (buffer[0] == '#')
155 for (int c = 0; c < strlen(buffer); c++)
157 // convert all spaces that are OUTSIDE quotes into hardspace (0xA0) as this will make them easier to
158 // search and replace later :)
159 if ((!in_quotes) && (buffer[c] == ' '))
161 if ((buffer[c] == '<') && (!in_quotes))
163 has_open_bracket = true;
164 if (strlen(buffer) == 1)
166 *errorstream << "Tag without identifier at " << filename << ":" << linenumber << endl;
170 else if ((tolower(buffer[c+1]) < 'a') || (tolower(buffer[c+1]) > 'z'))
172 *errorstream << "Invalid characters in identifier at " << filename << ":" << linenumber << endl;
177 if (buffer[c] == '"')
180 in_quotes = (!in_quotes);
182 if ((buffer[c] == '=') && (!in_quotes))
185 if (strlen(buffer) == c)
187 *errorstream << "Variable without a value at " << filename << ":" << linenumber << endl;
191 else if (buffer[c+1] != '"')
193 *errorstream << "Variable name not followed immediately by its value at " << filename << ":" << linenumber << endl;
199 *errorstream << "Value without a variable (line starts with '=') at " << filename << ":" << linenumber << endl;
203 else if (buffer[c-1] == '\xA0')
205 *errorstream << "Variable name not followed immediately by its value at " << filename << ":" << linenumber << endl;
211 // no quotes, and no equals. something freaky.
212 if ((!number_of_quotes) || (!number_of_equals) && (strlen(buffer)>2) && (buffer[0]=='<'))
214 *errorstream << "Malformed tag at " << filename << ":" << linenumber << endl;
218 // odd number of quotes. thats just wrong.
219 if ((number_of_quotes % 2) != 0)
221 *errorstream << "Missing \" at " << filename << ":" << linenumber << endl;
225 if (number_of_equals < (number_of_quotes/2))
227 *errorstream << "Missing '=' at " << filename << ":" << linenumber << endl;
229 if (number_of_equals > (number_of_quotes/2))
231 *errorstream << "Too many '=' at " << filename << ":" << linenumber << endl;
234 std::string parsedata = buffer;
235 // turn multispace into single space
236 while (parsedata.find("\xA0\xA0") != std::string::npos)
238 parsedata.erase(parsedata.find("\xA0\xA0"),1);
241 // turn our hardspace back into softspace
242 for (int d = 0; d < parsedata.length(); d++)
244 if (parsedata[d] == '\xA0')
248 // and we're done, the line is fine!
252 bool LoadConf(const char* filename, std::stringstream *target, std::stringstream* errorstream)
255 errorstream->str("");
257 FILE* conf = fopen(filename,"r");
258 if (!FileExists(filename))
260 *errorstream << "File " << filename << " not found." << endl;
268 if (fgets(buffer, MAXBUF, conf))
270 if ((!feof(conf)) && (buffer) && (strlen(buffer)))
272 if ((buffer[0] != '#') && (buffer[0] != '\r') && (buffer[0] != '\n'))
275 std::string data = ConfProcess(buffer,linenumber++,errorstream,error,filename);
292 /* Counts the number of tags of a certain type within the config file, e.g. to enumerate opers */
294 int EnumConf(std::stringstream *config, const char* tag)
297 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
298 int in_token, in_quotes, tptr, j, idx = 0;
301 const char* buf = config->str().c_str();
303 long len = strlen(buf);
313 if ((c == '#') && (lastc == '\n'))
315 while ((c != '\n') && (bptr<len))
321 if ((c == '<') && (!in_quotes))
336 in_quotes = (!in_quotes);
338 if ((c == '>') && (!in_quotes))
341 if (!strcmp(c_tag,tag))
343 /* correct tag, but wrong index */
353 if ((in_token) && (c != '\n') && (c != '\r'))
363 /* Counts the number of values within a certain tag */
365 int EnumValues(std::stringstream *config, const char* tag, int index)
368 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
369 int in_token, in_quotes, tptr, j, idx = 0;
372 bool correct_tag = false;
375 const char* buf = config->str().c_str();
377 long len = strlen(buf);
387 if ((c == '#') && (lastc == '\n'))
389 while ((c != '\n') && (bptr<len))
395 if ((c == '<') && (!in_quotes))
406 if ((!strcmp(c_tag,tag)) && (idx == index))
415 in_quotes = (!in_quotes);
418 if ( (correct_tag) && (!in_quotes) && ( (c == ' ') || (c == '\n') || (c == '\r') ) )
422 if ((c == '>') && (!in_quotes))
427 if (!strcmp(c_tag,tag))
429 /* correct tag, but wrong index */
439 if ((in_token) && (c != '\n') && (c != '\r'))
451 int ConfValueEnum(char* tag, std::stringstream* config)
453 return EnumConf(config,tag);
458 /* Retrieves a value from the config file. If there is more than one value of the specified
459 * key and section (e.g. for opers etc) then the index value specifies which to retreive, e.g.
461 * ConfValue("oper","name",2,result);
464 int ReadConf(std::stringstream *config, const char* tag, const char* var, int index, char *result)
467 char buffer[65535], c_tag[MAXBUF], c, lastc;
468 int in_token, in_quotes, tptr, j, idx = 0;
471 const char* buf = config->str().c_str();
473 long len = strlen(buf);
485 // FIX: Treat tabs as spaces
488 if ((c == '<') && (!in_quotes))
499 // FIX: Tab can follow a tagname as well as space.
500 } while ((c != ' ') && (c != 9));
504 in_quotes = (!in_quotes);
506 if ((c == '>') && (!in_quotes))
511 if (!strcmp(c_tag,tag))
513 if ((buffer) && (c_tag) && (var))
515 key = strstr(buffer,var);
518 /* value not found in tag */
536 for (j = 0; j < strlen(key); j++)
543 strlcpy(result,key,MAXBUF);
549 if (!strcmp(c_tag,tag))
551 /* correct tag, but wrong index */
561 if ((in_token) && (c != '\n') && (c != '\r'))
568 strcpy(result,""); // value or its tag not found at all
574 int ConfValue(char* tag, char* var, int index, char *result,std::stringstream *config)
576 ReadConf(config, tag, var, index, result);
582 /* This will bind a socket to a port. It works for UDP/TCP */
583 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
585 bzero((char *)&server,sizeof(server));
587 inet_aton(addr,&addy);
589 server.sin_family = AF_INET;
590 if (!strcmp(addr,""))
592 server.sin_addr.s_addr = htonl(INADDR_ANY);
596 server.sin_addr = addy;
599 server.sin_port = htons(port);
601 if (bind(sockfd,(struct sockaddr*)&server,sizeof(server))<0)
613 /* Open a TCP Socket */
614 int OpenTCPSocket (void)
618 struct linger linger = { 0 };
620 if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
624 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on));
625 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
628 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (const char*)&linger,sizeof(linger));