]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/inspircd_io.cpp
Updated to support services
[user/henk/code/inspircd.git] / src / inspircd_io.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2003 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 #ifdef __linux__ 
18 #include <sys/resource.h>
19 #endif
20
21 #include <sys/types.h>
22 #include <string>
23 #include <unistd.h>
24 #include <sstream>
25 #include <iostream>
26 #include <fstream>
27 #include "inspircd.h"
28 #include "inspircd_io.h"
29 #include "inspircd_util.h"
30
31 using namespace std;
32
33 extern FILE *log_file;
34
35 void WriteOpers(char* text, ...);
36
37 void Exit (int status)
38 {
39   if (log_file)
40         fclose(log_file);
41   send_error("Server shutdown.");
42   exit (status);
43 }
44
45 void Killed(int status)
46 {
47   if (log_file)
48         fclose(log_file);
49   send_error("Server terminated.");
50   exit(status);
51 }
52
53 void Rehash(int status)
54 {
55   ReadConfig();
56   WriteOpers("Rehashing config file %s due to SIGHUP",CONFIG_FILE);
57 }
58
59
60
61 void Start (void)
62 {
63   printf("\033[1;37mInspire Internet Relay Chat Server, compiled " __DATE__ " at " __TIME__ "\n");
64   printf("(C) ChatSpike Development team.\033[0;37m\n\n");
65   printf("\033[1;37mDevelopers:\033[0;37m     Brain, FrostyCoolSlug, RD\n");
66   printf("\033[1;37mDocumentation:\033[0;37m  FrostyCoolSlug, w00t\n");
67   printf("\033[1;37mTesters:\033[0;37m        typobox43, piggles, Lord_Zathras, CC\n");
68   printf("\033[1;37mName concept:\033[0;37m   Lord_Zathras\n\n");
69 }
70
71
72 void DeadPipe(int status)
73 {
74   signal (SIGPIPE, DeadPipe);
75 }
76
77 int DaemonSeed (void)
78 {
79   int childpid;
80   signal (SIGALRM, SIG_IGN);
81   signal (SIGHUP, Rehash);
82   signal (SIGPIPE, DeadPipe);
83   signal (SIGTERM, Exit);
84   signal (SIGABRT, Exit);
85   signal (SIGSEGV, Error);
86   signal (SIGURG, Exit);
87   signal (SIGKILL, Exit);
88   if ((childpid = fork ()) < 0)
89     return (ERROR);
90   else if (childpid > 0)
91     exit (0);
92   setsid ();
93   umask (077);
94   /* close stdout, stdin, stderr */
95   close(0);
96   close(1);
97   close(2);
98
99   #ifdef __linux__ 
100   setpriority(PRIO_PROCESS,(int)getpid(),15); /* ircd sets to low process priority so it doesnt hog the box */
101   #endif
102   
103   return (TRUE);
104 }
105
106
107 /* Make Sure Modules Are Avaliable!
108  * (BugFix By Craig.. See? I do work! :p)
109  * Modified by brain, requires const char*
110  * to work with other API functions
111  */
112
113 bool FileExists (const char* file)
114 {
115   FILE *input;
116   
117   if ((input = fopen (file, "r")) == NULL) { return(false); }
118   else { fclose (input); return(true); }
119 }
120
121
122 bool LoadConf(const char* filename, std::stringstream *target)
123 {
124         FILE* conf = fopen(filename,"r");
125         if (!FileExists(filename))
126         {
127                 return false;
128         }
129         char buffer[MAXBUF];
130         if (conf)
131         {
132                 target->clear();
133                 while (!feof(conf))
134                 {
135                         if (fgets(buffer, MAXBUF, conf))
136                         {
137                                 if ((!feof(conf)) && (buffer) && (strlen(buffer)))
138                                 {
139                                         if (buffer[0] != '#')
140                                         {
141                                                 *target << std::string(buffer);
142                                         }
143                                 }
144                         }
145                 }
146                 fclose(conf);
147         }
148         target->seekg(0);
149         return true;
150 }
151
152 /* Counts the number of tags of a certain type within the config file, e.g. to enumerate opers */
153
154 int EnumConf(std::stringstream *config, const char* tag)
155 {
156         int ptr = 0;
157         char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
158         int in_token, in_quotes, tptr, j, idx = 0;
159         char* key;
160
161         const char* buf = config->str().c_str();
162         long bptr = 0;
163         long len = strlen(buf);
164         
165         ptr = 0;
166         in_token = 0;
167         in_quotes = 0;
168         lastc = '\0';
169         while (bptr<len)
170         {
171                 lastc = c;
172                 c = buf[bptr++];
173                 if ((c == '#') && (lastc == '\n'))
174                 {
175                         while ((c != '\n') && (bptr<len))
176                         {
177                                 lastc = c;
178                                 c = buf[bptr++];
179                         }
180                 }
181                 if ((c == '<') && (!in_quotes))
182                 {
183                         tptr = 0;
184                         in_token = 1;
185                         do {
186                                 c = buf[bptr++];
187                                 if (c != ' ')
188                                 {
189                                         c_tag[tptr++] = c;
190                                         c_tag[tptr] = '\0';
191                                 }
192                         } while (c != ' ');
193                 }
194                 if (c == '"')
195                 {
196                         in_quotes = (!in_quotes);
197                 }
198                 if ((c == '>') && (!in_quotes))
199                 {
200                         in_token = 0;
201                         if (!strcmp(c_tag,tag))
202                         {
203                                 /* correct tag, but wrong index */
204                                 idx++;
205                         }
206                         c_tag[0] = '\0';
207                         buffer[0] = '\0';
208                         ptr = 0;
209                         tptr = 0;
210                 }
211                 if (c != '>')
212                 {
213                         if ((in_token) && (c != '\n') && (c != '\r'))
214                         {
215                                 buffer[ptr++] = c;
216                                 buffer[ptr] = '\0';
217                         }
218                 }
219         }
220         return idx;
221 }
222
223 /* Counts the number of values within a certain tag */
224
225 int EnumValues(std::stringstream *config, const char* tag, int index)
226 {
227         int ptr = 0;
228         char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
229         int in_token, in_quotes, tptr, j, idx = 0;
230         char* key;
231         
232         bool correct_tag = false;
233         int num_items = 0;
234
235         const char* buf = config->str().c_str();
236         long bptr = 0;
237         long len = strlen(buf);
238         
239         ptr = 0;
240         in_token = 0;
241         in_quotes = 0;
242         lastc = '\0';
243         while (bptr<len)
244         {
245                 lastc = c;
246                 c = buf[bptr++];
247                 if ((c == '#') && (lastc == '\n'))
248                 {
249                         while ((c != '\n') && (bptr<len))
250                         {
251                                 lastc = c;
252                                 c = buf[bptr++];
253                         }
254                 }
255                 if ((c == '<') && (!in_quotes))
256                 {
257                         tptr = 0;
258                         in_token = 1;
259                         do {
260                                 c = buf[bptr++];
261                                 if (c != ' ')
262                                 {
263                                         c_tag[tptr++] = c;
264                                         c_tag[tptr] = '\0';
265                                         
266                                         if ((!strcmp(c_tag,tag)) && (idx == index))
267                                         {
268                                                 correct_tag = true;
269                                         }
270                                 }
271                         } while (c != ' ');
272                 }
273                 if (c == '"')
274                 {
275                         in_quotes = (!in_quotes);
276                 }
277                 
278                 if ( (correct_tag) && (!in_quotes) && ( (c == ' ') || (c == '\n') || (c == '\r') ) )
279                 {
280                         num_items++;
281                 }
282                 if ((c == '>') && (!in_quotes))
283                 {
284                         in_token = 0;
285                         if (correct_tag)
286                                 correct_tag = false;
287                         if (!strcmp(c_tag,tag))
288                         {
289                                 /* correct tag, but wrong index */
290                                 idx++;
291                         }
292                         c_tag[0] = '\0';
293                         buffer[0] = '\0';
294                         ptr = 0;
295                         tptr = 0;
296                 }
297                 if (c != '>')
298                 {
299                         if ((in_token) && (c != '\n') && (c != '\r'))
300                         {
301                                 buffer[ptr++] = c;
302                                 buffer[ptr] = '\0';
303                         }
304                 }
305         }
306         return num_items+1;
307 }
308
309
310
311 int ConfValueEnum(char* tag, std::stringstream* config)
312 {
313         return EnumConf(config,tag);
314 }
315
316
317
318 /* Retrieves a value from the config file. If there is more than one value of the specified
319  * key and section (e.g. for opers etc) then the index value specifies which to retreive, e.g.
320  *
321  * ConfValue("oper","name",2,result);
322  */
323
324 int ReadConf(std::stringstream *config, const char* tag, const char* var, int index, char *result)
325 {
326         int ptr = 0;
327         char buffer[65535], c_tag[MAXBUF], c, lastc;
328         int in_token, in_quotes, tptr, j, idx = 0;
329         char* key;
330
331         const char* buf = config->str().c_str();
332         long bptr = 0;
333         long len = strlen(buf);
334         log(DEBUG,"Data length: %d",len);
335         
336         ptr = 0;
337         in_token = 0;
338         in_quotes = 0;
339         lastc = '\0';
340         c_tag[0] = '\0';
341         buffer[0] = '\0';
342         while (bptr<len)
343         {
344                 lastc = c;
345                 c = buf[bptr++];
346                 // FIX: Treat tabs as spaces
347                 if (c == 9)
348                         c = 32;
349                 if ((c == '<') && (!in_quotes))
350                 {
351                         tptr = 0;
352                         in_token = 1;
353                         do {
354                                 c = buf[bptr++];
355                                 if (c != ' ')
356                                 {
357                                         c_tag[tptr++] = c;
358                                         c_tag[tptr] = '\0';
359                                 }
360                         // FIX: Tab can follow a tagname as well as space.
361                         } while ((c != ' ') && (c != 9));
362                 }
363                 if (c == '"')
364                 {
365                         in_quotes = (!in_quotes);
366                 }
367                 if ((c == '>') && (!in_quotes))
368                 {
369                         in_token = 0;
370                         if (idx == index)
371                         {
372                                 if (!strcmp(c_tag,tag))
373                                 {
374                                         if ((buffer) && (c_tag) && (var))
375                                         {
376                                                 key = strstr(buffer,var);
377                                                 if (!key)
378                                                 {
379                                                         /* value not found in tag */
380                                                         strcpy(result,"");
381                                                         log(DEBUG,"ReadConf: value '%s' was not found in tag",var);
382                                                         return 0;
383                                                 }
384                                                 else
385                                                 {
386                                                         key+=strlen(var);
387                                                         while (key[0] !='"')
388                                                         {
389                                                                 if (!strlen(key))
390                                                                 {
391                                                                         /* missing quote */
392                                                                         strcpy(result,"");
393                                                                         log(DEBUG,"ReadConf: possible missing quote!");
394                                                                         return 0;
395                                                                 }
396                                                                 key++;
397                                                         }
398                                                         key++;
399                                                         for (j = 0; j < strlen(key); j++)
400                                                         {
401                                                                 if (key[j] == '"')
402                                                                 {
403                                                                         key[j] = '\0';
404                                                                 }
405                                                         }
406                                                         strcpy(result,key);
407                                                         log(DEBUG,"ReadConf: Got value '%s'",result);
408                                                         return 1;
409                                                 }
410                                         }
411                                 }
412                         }
413                         if (!strcmp(c_tag,tag))
414                         {
415                                 log(DEBUG,"Tag name correct but index value incorrect");
416                                 /* correct tag, but wrong index */
417                                 idx++;
418                         }
419                         c_tag[0] = '\0';
420                         buffer[0] = '\0';
421                         ptr = 0;
422                         tptr = 0;
423                 }
424                 if (c != '>')
425                 {
426                         if ((in_token) && (c != '\n') && (c != '\r'))
427                         {
428                                 buffer[ptr++] = c;
429                                 buffer[ptr] = '\0';
430                         }
431                 }
432         }
433         log(DEBUG,"ReadConf: neither value '%s' or tag '%s' were found at all!",var,tag);
434         strcpy(result,""); // value or its tag not found at all
435         return 0;
436 }
437
438
439
440 int ConfValue(char* tag, char* var, int index, char *result,std::stringstream *config)
441 {
442         ReadConf(config, tag, var, index, result);
443         return 0;
444 }
445
446
447
448 /* This will bind a socket to a port. It works for UDP/TCP */
449 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
450 {
451   bzero((char *)&server,sizeof(server));
452   struct in_addr addy;
453   inet_aton(addr,&addy);
454
455   server.sin_family = AF_INET;
456   if (!strcmp(addr,""))
457   {
458           server.sin_addr.s_addr = htonl(INADDR_ANY);
459   }
460   else
461   {
462           server.sin_addr = addy;
463   }
464
465   server.sin_port = htons(port);
466
467   if (bind(sockfd,(struct sockaddr*)&server,sizeof(server))<0)
468   {
469     return(ERROR);
470   }
471   else
472   {
473     listen(sockfd,5);
474     return(TRUE);
475   }
476 }
477
478
479 /* Open a TCP Socket */
480 int OpenTCPSocket (void)
481 {
482   int sockfd;
483   int on = 0;
484   struct linger linger = { 0 };
485   
486   if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
487     return (ERROR);
488   else
489   {
490     setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on));
491     /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
492     linger.l_onoff = 1;
493     linger.l_linger = 0;
494     setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (const char*)&linger,sizeof(linger));
495     return (sockfd);
496   }
497 }
498