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
145 while ((buffer[0] == ' ') && (strlen(buffer)>0)) buffer++;
146 while ((buffer[strlen(buffer)-1] == ' ') && (strlen(buffer)>0)) buffer[strlen(buffer)-1] = '\0';
147 // empty lines are syntactically valid
148 if (!strcmp(buffer,""))
150 else if (buffer[0] == '#')
152 for (int c = 0; c < strlen(buffer); c++)
156 // convert all spaces that are OUTSIDE quotes into hardspace (0xA0) as this will make them easier to
157 // search and replace later :)
158 if ((!in_quotes) && (buffer[c] == ' '))
160 if ((buffer[c] == '<') && (!in_quotes))
162 has_open_bracket = true;
163 if (strlen(buffer) == 1)
165 *errorstream << "Tag without identifier at " << filename << ":" << linenumber << endl;
169 else if ((tolower(buffer[c+1]) < 'a') || (tolower(buffer[c+1]) > 'z'))
171 *errorstream << "Invalid characters in identifier at " << filename << ":" << linenumber << endl;
176 if (buffer[c] == '"')
179 in_quotes = (!in_quotes);
181 if ((buffer[c] == '=') && (!in_quotes))
184 if (strlen(buffer) == c)
186 *errorstream << "Variable without a value at " << filename << ":" << linenumber << endl;
190 else if (buffer[c+1] != '"')
192 *errorstream << "Variable name not followed immediately by its value at " << filename << ":" << linenumber << endl;
198 *errorstream << "Value without a variable (line starts with '=') at " << filename << ":" << linenumber << endl;
202 else if (buffer[c-1] == '\xA0')
204 *errorstream << "Variable name not followed immediately by its value at " << filename << ":" << linenumber << endl;
210 // no quotes, and no equals. something freaky.
211 if ((!number_of_quotes) || (!number_of_equals))
213 *errorstream << "Malformed tag at " << filename << ":" << linenumber << endl;
217 // odd number of quotes. thats just wrong.
218 if ((number_of_quotes % 2) != 0)
220 *errorstream << "Missing \" at " << filename << ":" << linenumber << endl;
224 if (number_of_equals < (number_of_quotes/2))
226 *errorstream << "Missing '=' at " << filename << ":" << linenumber << endl;
228 if (number_of_equals > (number_of_quotes/2))
230 *errorstream << "Too many '=' at " << filename << ":" << linenumber << endl;
233 std::string parsedata = buffer;
234 // turn multispace into single space
235 while (parsedata.find("\xA0\xA0") != std::string::npos)
237 parsedata.erase(parsedata.find("\xA0\xA0"),1);
240 // turn our hardspace back into softspace
241 for (int d = 0; d < parsedata.length(); d++)
243 if (parsedata[d] == '\xA0')
247 // and we're done, the line is fine!
251 bool LoadConf(const char* filename, std::stringstream *target, std::stringstream* errorstream)
254 errorstream->str("");
256 FILE* conf = fopen(filename,"r");
257 if (!FileExists(filename))
259 *errorstream << "File " << filename << " not found." << endl;
267 if (fgets(buffer, MAXBUF, conf))
269 if ((!feof(conf)) && (buffer) && (strlen(buffer)))
271 if ((buffer[0] != '#') && (buffer[0] != '\r') && (buffer[0] != '\n'))
274 std::string data = ConfProcess(buffer,linenumber++,errorstream,error,filename);
291 /* Counts the number of tags of a certain type within the config file, e.g. to enumerate opers */
293 int EnumConf(std::stringstream *config, const char* tag)
296 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
297 int in_token, in_quotes, tptr, j, idx = 0;
300 const char* buf = config->str().c_str();
302 long len = strlen(buf);
312 if ((c == '#') && (lastc == '\n'))
314 while ((c != '\n') && (bptr<len))
320 if ((c == '<') && (!in_quotes))
335 in_quotes = (!in_quotes);
337 if ((c == '>') && (!in_quotes))
340 if (!strcmp(c_tag,tag))
342 /* correct tag, but wrong index */
352 if ((in_token) && (c != '\n') && (c != '\r'))
362 /* Counts the number of values within a certain tag */
364 int EnumValues(std::stringstream *config, const char* tag, int index)
367 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
368 int in_token, in_quotes, tptr, j, idx = 0;
371 bool correct_tag = false;
374 const char* buf = config->str().c_str();
376 long len = strlen(buf);
386 if ((c == '#') && (lastc == '\n'))
388 while ((c != '\n') && (bptr<len))
394 if ((c == '<') && (!in_quotes))
405 if ((!strcmp(c_tag,tag)) && (idx == index))
414 in_quotes = (!in_quotes);
417 if ( (correct_tag) && (!in_quotes) && ( (c == ' ') || (c == '\n') || (c == '\r') ) )
421 if ((c == '>') && (!in_quotes))
426 if (!strcmp(c_tag,tag))
428 /* correct tag, but wrong index */
438 if ((in_token) && (c != '\n') && (c != '\r'))
450 int ConfValueEnum(char* tag, std::stringstream* config)
452 return EnumConf(config,tag);
457 /* Retrieves a value from the config file. If there is more than one value of the specified
458 * key and section (e.g. for opers etc) then the index value specifies which to retreive, e.g.
460 * ConfValue("oper","name",2,result);
463 int ReadConf(std::stringstream *config, const char* tag, const char* var, int index, char *result)
466 char buffer[65535], c_tag[MAXBUF], c, lastc;
467 int in_token, in_quotes, tptr, j, idx = 0;
470 const char* buf = config->str().c_str();
472 long len = strlen(buf);
484 // FIX: Treat tabs as spaces
487 if ((c == '<') && (!in_quotes))
498 // FIX: Tab can follow a tagname as well as space.
499 } while ((c != ' ') && (c != 9));
503 in_quotes = (!in_quotes);
505 if ((c == '>') && (!in_quotes))
510 if (!strcmp(c_tag,tag))
512 if ((buffer) && (c_tag) && (var))
514 key = strstr(buffer,var);
517 /* value not found in tag */
535 for (j = 0; j < strlen(key); j++)
542 strlcpy(result,key,MAXBUF);
548 if (!strcmp(c_tag,tag))
550 /* correct tag, but wrong index */
560 if ((in_token) && (c != '\n') && (c != '\r'))
567 strcpy(result,""); // value or its tag not found at all
573 int ConfValue(char* tag, char* var, int index, char *result,std::stringstream *config)
575 ReadConf(config, tag, var, index, result);
581 /* This will bind a socket to a port. It works for UDP/TCP */
582 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
584 bzero((char *)&server,sizeof(server));
586 inet_aton(addr,&addy);
588 server.sin_family = AF_INET;
589 if (!strcmp(addr,""))
591 server.sin_addr.s_addr = htonl(INADDR_ANY);
595 server.sin_addr = addy;
598 server.sin_port = htons(port);
600 if (bind(sockfd,(struct sockaddr*)&server,sizeof(server))<0)
612 /* Open a TCP Socket */
613 int OpenTCPSocket (void)
617 struct linger linger = { 0 };
619 if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
623 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on));
624 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
627 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (const char*)&linger,sizeof(linger));