ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/branches/8.2.x/src/ircd.c
Revision: 3930
Committed: Mon Jun 9 14:58:12 2014 UTC (9 years, 9 months ago) by michael
Content type: text/x-csrc
File size: 16438 byte(s)
Log Message:
- Greatly speedup d-line lookup. Instead of testing every single client against
  every existing d-line just check the just added ban against connected clients.
  Also now check d-line _before_ looking for a matching exempt{}

File Contents

# User Rev Content
1 adx 30 /*
2 michael 2916 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 adx 30 *
4 michael 2916 * Copyright (c) 1997-2014 ircd-hybrid development team
5 adx 30 *
6     * This program is free software; you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19     * USA
20     */
21    
22 michael 2916 /*! \file ircd.c
23     * \brief Starts up and runs the ircd.
24     * \version $Id$
25     */
26    
27 adx 30 #include "stdinc.h"
28 michael 3347 #include "user.h"
29 michael 1011 #include "list.h"
30 adx 30 #include "ircd.h"
31     #include "channel.h"
32     #include "client.h"
33     #include "event.h"
34     #include "fdlist.h"
35     #include "hash.h"
36     #include "irc_string.h"
37     #include "ircd_signal.h"
38 michael 3321 #include "gline.h"
39 adx 30 #include "motd.h"
40 michael 1632 #include "conf.h"
41 adx 30 #include "hostmask.h"
42     #include "parse.h"
43 michael 3322 #include "res.h"
44 adx 30 #include "restart.h"
45 michael 982 #include "rng_mt.h"
46 michael 3324 #include "auth.h"
47 adx 30 #include "s_bsd.h"
48 michael 1309 #include "log.h"
49 michael 3347 #include "server.h" /* try_connections */
50 adx 30 #include "send.h"
51     #include "whowas.h"
52     #include "modules.h"
53     #include "memory.h"
54 michael 1654 #include "mempool.h"
55 adx 30 #include "ircd_getopt.h"
56     #include "supported.h"
57 michael 876 #include "watch.h"
58 michael 1622 #include "conf_db.h"
59 michael 1632 #include "conf_class.h"
60 adx 30
61 michael 1858
62     #ifdef HAVE_LIBGEOIP
63     GeoIP *geoip_ctx;
64     #endif
65 michael 2872
66 adx 30 /* /quote set variables */
67     struct SetOptions GlobalSetOptions;
68 michael 2872 struct Counter Count;
69     struct ServerState_t server_state;
70 michael 896 struct ServerStatistics ServerStats;
71 adx 30 struct timeval SystemTime;
72     struct Client me; /* That's me */
73     struct LocalUser meLocalUser; /* That's also part of me */
74    
75     const char *logFileName = LPATH;
76     const char *pidFileName = PPATH;
77    
78     char **myargv;
79    
80     int dorehash = 0;
81     int doremotd = 0;
82    
83     /* Set to zero because it should be initialized later using
84     * initialize_server_capabs
85     */
86 michael 3906 unsigned int default_server_capabs;
87 michael 1013 unsigned int splitmode;
88     unsigned int splitchecking;
89     unsigned int split_users;
90 adx 30 unsigned int split_servers;
91    
92     /*
93     * print_startup - print startup information
94     */
95     static void
96     print_startup(int pid)
97     {
98 michael 2646 printf("ircd: version %s(%s)\n", ircd_version, serno);
99 adx 30 printf("ircd: pid %d\n", pid);
100     printf("ircd: running in %s mode from %s\n", !server_state.foreground ? "background"
101     : "foreground", ConfigFileEntry.dpath);
102     }
103    
104     static void
105     make_daemon(void)
106     {
107     int pid;
108    
109     if ((pid = fork()) < 0)
110     {
111     perror("fork");
112     exit(EXIT_FAILURE);
113     }
114     else if (pid > 0)
115     {
116     print_startup(pid);
117     exit(EXIT_SUCCESS);
118     }
119    
120     setsid();
121     }
122    
123     static int printVersion = 0;
124    
125 michael 3215 static struct lgetopt myopts[] =
126     {
127 michael 2916 {"configfile", &ConfigFileEntry.configfile,
128 adx 30 STRING, "File to use for ircd.conf"},
129 michael 1718 {"glinefile", &ConfigFileEntry.glinefile,
130     STRING, "File to use for gline database"},
131 michael 2916 {"klinefile", &ConfigFileEntry.klinefile,
132 michael 1718 STRING, "File to use for kline database"},
133     {"dlinefile", &ConfigFileEntry.dlinefile,
134     STRING, "File to use for dline database"},
135 michael 2916 {"xlinefile", &ConfigFileEntry.xlinefile,
136 michael 1718 STRING, "File to use for xline database"},
137     {"resvfile", &ConfigFileEntry.resvfile,
138     STRING, "File to use for resv database"},
139 michael 2916 {"logfile", &logFileName,
140 adx 30 STRING, "File to use for ircd.log"},
141     {"pidfile", &pidFileName,
142     STRING, "File to use for process ID"},
143 michael 2916 {"foreground", &server_state.foreground,
144 adx 30 YESNO, "Run in foreground (don't detach)"},
145 michael 2916 {"version", &printVersion,
146 adx 30 YESNO, "Print version and exit"},
147     {"help", NULL, USAGE, "Print this text"},
148     {NULL, NULL, STRING, NULL},
149     };
150    
151     void
152     set_time(void)
153     {
154 michael 2978 struct timeval newtime = { .tv_sec = 0, .tv_usec = 0 };
155 adx 30
156     if (gettimeofday(&newtime, NULL) == -1)
157     {
158 michael 1247 ilog(LOG_TYPE_IRCD, "Clock Failure (%s), TS can be corrupted",
159 adx 30 strerror(errno));
160 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
161 adx 30 "Clock Failure (%s), TS can be corrupted",
162     strerror(errno));
163 michael 3167 server_die("Clock Failure", 1);
164 adx 30 }
165    
166     if (newtime.tv_sec < CurrentTime)
167     {
168 michael 2980 ilog(LOG_TYPE_IRCD, "System clock is running backwards - (%lu < %lu)",
169     (unsigned long)newtime.tv_sec, (unsigned long)CurrentTime);
170     sendto_realops_flags(UMODE_DEBUG, L_ALL, SEND_NOTICE,
171     "System clock is running backwards - (%lu < %lu)",
172     (unsigned long)newtime.tv_sec,
173     (unsigned long)CurrentTime);
174 adx 30 set_back_events(CurrentTime - newtime.tv_sec);
175     }
176    
177     SystemTime.tv_sec = newtime.tv_sec;
178     SystemTime.tv_usec = newtime.tv_usec;
179     }
180    
181     static void
182     io_loop(void)
183     {
184 michael 3215 while (1)
185 adx 30 {
186     if (listing_client_list.head)
187     {
188     dlink_node *ptr = NULL, *ptr_next = NULL;
189     DLINK_FOREACH_SAFE(ptr, ptr_next, listing_client_list.head)
190 michael 3288 safe_list_channels(ptr->data, 0);
191 adx 30 }
192    
193     /* Run pending events, then get the number of seconds to the next
194     * event
195     */
196     while (eventNextTime() <= CurrentTime)
197     eventRun();
198    
199     comm_select();
200     exit_aborted_clients();
201     free_exited_clients();
202    
203     /* Check to see whether we have to rehash the configuration .. */
204     if (dorehash)
205     {
206     rehash(1);
207     dorehash = 0;
208     }
209 michael 3215
210 adx 30 if (doremotd)
211     {
212 michael 2150 motd_recache();
213 michael 1618 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
214 michael 3065 "Got signal SIGUSR1, reloading motd file(s)");
215 adx 30 doremotd = 0;
216     }
217     }
218     }
219    
220     /* initalialize_global_set_options()
221     *
222     * inputs - none
223     * output - none
224 michael 2916 * side effects - This sets all global set options needed
225 adx 30 */
226     static void
227     initialize_global_set_options(void)
228     {
229     memset(&GlobalSetOptions, 0, sizeof(GlobalSetOptions));
230    
231     GlobalSetOptions.autoconn = 1;
232     GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME;
233     GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT;
234    
235     if (ConfigFileEntry.default_floodcount)
236     GlobalSetOptions.floodcount = ConfigFileEntry.default_floodcount;
237     else
238     GlobalSetOptions.floodcount = 10;
239    
240     /* XXX I have no idea what to try here - Dianora */
241     GlobalSetOptions.joinfloodcount = 16;
242     GlobalSetOptions.joinfloodtime = 8;
243    
244     split_servers = ConfigChannel.default_split_server_count;
245     split_users = ConfigChannel.default_split_user_count;
246    
247     if (split_users && split_servers && (ConfigChannel.no_create_on_split ||
248     ConfigChannel.no_join_on_split))
249     {
250     splitmode = 1;
251     splitchecking = 1;
252     }
253    
254     GlobalSetOptions.ident_timeout = IDENT_TIMEOUT;
255     /* End of global set options */
256     }
257    
258     /* initialize_server_capabs()
259     *
260     * inputs - none
261     * output - none
262     */
263     static void
264     initialize_server_capabs(void)
265     {
266     add_capability("QS", CAP_QS, 1);
267     add_capability("EOB", CAP_EOB, 1);
268 michael 1117 add_capability("TS6", CAP_TS6, 0);
269 adx 30 add_capability("CLUSTER", CAP_CLUSTER, 1);
270 michael 1196 add_capability("SVS", CAP_SVS, 1);
271 michael 3267 add_capability("CHW", CAP_CHW, 1);
272 adx 30 add_capability("HOPS", CAP_HOPS, 1);
273     }
274    
275     /* write_pidfile()
276     *
277     * inputs - filename+path of pid file
278     * output - NONE
279     * side effects - write the pid of the ircd to filename
280     */
281     static void
282     write_pidfile(const char *filename)
283     {
284 michael 1325 FILE *fb;
285 adx 30
286 michael 1325 if ((fb = fopen(filename, "w")))
287 adx 30 {
288 michael 2691 char buff[IRCD_BUFSIZE];
289 adx 30 unsigned int pid = (unsigned int)getpid();
290    
291 michael 1325 snprintf(buff, sizeof(buff), "%u\n", pid);
292    
293     if ((fputs(buff, fb) == -1))
294 michael 1247 ilog(LOG_TYPE_IRCD, "Error writing %u to pid file %s (%s)",
295 adx 30 pid, filename, strerror(errno));
296    
297 michael 1325 fclose(fb);
298 adx 30 }
299     else
300     {
301 michael 1247 ilog(LOG_TYPE_IRCD, "Error opening pid file %s", filename);
302 adx 30 }
303     }
304    
305     /* check_pidfile()
306     *
307     * inputs - filename+path of pid file
308     * output - none
309     * side effects - reads pid from pidfile and checks if ircd is in process
310     * list. if it is, gracefully exits
311     * -kre
312     */
313     static void
314     check_pidfile(const char *filename)
315     {
316 michael 1325 FILE *fb;
317 michael 2691 char buff[IRCD_BUFSIZE];
318 adx 30 pid_t pidfromfile;
319    
320     /* Don't do logging here, since we don't have log() initialised */
321 michael 1325 if ((fb = fopen(filename, "r")))
322 adx 30 {
323 michael 1325 if (fgets(buff, 20, fb) == NULL)
324 adx 30 {
325     /* log(L_ERROR, "Error reading from pid file %s (%s)", filename,
326     * strerror(errno));
327     */
328     }
329     else
330     {
331     pidfromfile = atoi(buff);
332    
333     if (!kill(pidfromfile, 0))
334     {
335     /* log(L_ERROR, "Server is already running"); */
336     printf("ircd: daemon is already running\n");
337     exit(-1);
338     }
339     }
340    
341 michael 1325 fclose(fb);
342 adx 30 }
343     else if (errno != ENOENT)
344     {
345     /* log(L_ERROR, "Error opening pid file %s", filename); */
346     }
347     }
348    
349     /* setup_corefile()
350     *
351     * inputs - nothing
352     * output - nothing
353     * side effects - setups corefile to system limits.
354     * -kre
355     */
356     static void
357     setup_corefile(void)
358     {
359     #ifdef HAVE_SYS_RESOURCE_H
360     struct rlimit rlim; /* resource limits */
361    
362     /* Set corefilesize to maximum */
363     if (!getrlimit(RLIMIT_CORE, &rlim))
364     {
365     rlim.rlim_cur = rlim.rlim_max;
366     setrlimit(RLIMIT_CORE, &rlim);
367     }
368     #endif
369     }
370    
371 michael 2228 #ifdef HAVE_LIBCRYPTO
372     static int
373     always_accept_verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx)
374     {
375     return 1;
376     }
377     #endif
378    
379 adx 30 /* init_ssl()
380     *
381     * inputs - nothing
382     * output - nothing
383     * side effects - setups SSL context.
384     */
385     static void
386 michael 1798 ssl_init(void)
387 adx 30 {
388     #ifdef HAVE_LIBCRYPTO
389 michael 2914 const unsigned char session_id[] = "ircd-hybrid";
390    
391 adx 30 SSL_load_error_strings();
392     SSLeay_add_ssl_algorithms();
393    
394 michael 967 if ((ServerInfo.server_ctx = SSL_CTX_new(SSLv23_server_method())) == NULL)
395 adx 30 {
396     const char *s;
397    
398 michael 1303 fprintf(stderr, "ERROR: Could not initialize the SSL Server context -- %s\n",
399 adx 30 s = ERR_lib_error_string(ERR_get_error()));
400 michael 1303 ilog(LOG_TYPE_IRCD, "ERROR: Could not initialize the SSL Server context -- %s\n", s);
401 adx 30 }
402    
403 michael 1316 SSL_CTX_set_options(ServerInfo.server_ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TLSv1);
404 michael 967 SSL_CTX_set_options(ServerInfo.server_ctx, SSL_OP_TLS_ROLLBACK_BUG|SSL_OP_ALL);
405 michael 2228 SSL_CTX_set_verify(ServerInfo.server_ctx, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,
406     always_accept_verify_cb);
407 michael 2914 SSL_CTX_set_session_id_context(ServerInfo.server_ctx, session_id, sizeof(session_id) - 1);
408 adx 30
409 michael 1303 if ((ServerInfo.client_ctx = SSL_CTX_new(SSLv23_client_method())) == NULL)
410     {
411     const char *s;
412    
413     fprintf(stderr, "ERROR: Could not initialize the SSL Client context -- %s\n",
414     s = ERR_lib_error_string(ERR_get_error()));
415     ilog(LOG_TYPE_IRCD, "ERROR: Could not initialize the SSL Client context -- %s\n", s);
416     }
417    
418 michael 1316 SSL_CTX_set_options(ServerInfo.client_ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TLSv1);
419 michael 1303 SSL_CTX_set_options(ServerInfo.client_ctx, SSL_OP_TLS_ROLLBACK_BUG|SSL_OP_ALL);
420 michael 2253 SSL_CTX_set_verify(ServerInfo.client_ctx, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,
421 michael 2228 always_accept_verify_cb);
422 adx 30 #endif /* HAVE_LIBCRYPTO */
423     }
424    
425     int
426     main(int argc, char *argv[])
427     {
428 michael 2253 /* Check to see if the user is running us as root, which is a nono */
429 adx 30 if (geteuid() == 0)
430     {
431 michael 3526 fprintf(stderr, "ERROR: This server won't run as root/superuser\n");
432 michael 982 return -1;
433 adx 30 }
434    
435     /* Setup corefile size immediately after boot -kre */
436     setup_corefile();
437    
438     /* save server boot time right away, so getrusage works correctly */
439     set_time();
440    
441 michael 982 /* It ain't random, but it ought to be a little harder to guess */
442     init_genrand(SystemTime.tv_sec ^ (SystemTime.tv_usec | (getpid() << 20)));
443    
444 adx 30 me.localClient = &meLocalUser;
445 michael 2253 dlinkAdd(&me, &me.node, &global_client_list); /* Pointer to beginning
446 adx 30 of Client list */
447     ConfigFileEntry.dpath = DPATH;
448 michael 3239 ConfigFileEntry.spath = SPATH;
449     ConfigFileEntry.mpath = MPATH;
450 michael 1702 ConfigFileEntry.configfile = CPATH; /* Server configuration file */
451     ConfigFileEntry.klinefile = KPATH; /* Server kline file */
452 michael 1718 ConfigFileEntry.glinefile = GPATH; /* Server gline file */
453 michael 1702 ConfigFileEntry.xlinefile = XPATH; /* Server xline file */
454     ConfigFileEntry.dlinefile = DLPATH; /* dline file */
455     ConfigFileEntry.resvfile = RESVPATH; /* resv file */
456    
457 adx 30 myargv = argv;
458     umask(077); /* better safe than sorry --SRB */
459    
460     parseargs(&argc, &argv, myopts);
461    
462     if (printVersion)
463     {
464 michael 2646 printf("ircd: version %s(%s)\n", ircd_version, serno);
465 adx 30 exit(EXIT_SUCCESS);
466     }
467    
468     if (chdir(ConfigFileEntry.dpath))
469     {
470     perror("chdir");
471     exit(EXIT_FAILURE);
472     }
473    
474 michael 1798 ssl_init();
475 adx 30
476     if (!server_state.foreground)
477     {
478     make_daemon();
479     close_standard_fds(); /* this needs to be before init_netio()! */
480     }
481     else
482     print_startup(getpid());
483    
484     setup_signals();
485    
486     /* Init the event subsystem */
487     eventInit();
488 michael 2253
489 adx 30 /* We need this to initialise the fd array before anything else */
490     fdlist_init();
491 michael 1831 log_set_file(LOG_TYPE_IRCD, 0, logFileName);
492 adx 30 check_can_use_v6();
493 michael 2632 init_netio(); /* This needs to be setup early ! -- adrian */
494 michael 2253
495 adx 30 /* Check if there is pidfile and daemon already running */
496     check_pidfile(pidFileName);
497    
498 michael 1654 mp_pool_init();
499 adx 30 init_dlink_nodes();
500 michael 2159 init_isupport();
501 adx 30 dbuf_init();
502 michael 1798 hash_init();
503 adx 30 init_ip_hash_table(); /* client host ip hash table */
504     init_host_hash(); /* Host-hashtable. */
505 michael 1798 client_init();
506 michael 1632 class_init();
507 michael 1358 whowas_init();
508 michael 876 watch_init();
509 michael 1798 auth_init(); /* Initialise the auth code */
510 michael 998 init_resolver(); /* Needs to be setup before the io loop */
511 michael 1404 modules_init();
512 adx 30 read_conf_files(1); /* cold start init conf files */
513     init_uid();
514     initialize_server_capabs(); /* Set up default_server_capabs */
515     initialize_global_set_options();
516 michael 1798 channel_init();
517 michael 2216 read_links_file();
518 michael 2150 motd_init();
519 michael 1858 #ifdef HAVE_LIBGEOIP
520     geoip_ctx = GeoIP_new(GEOIP_MEMORY_CACHE);
521     #endif
522 adx 30
523 michael 1115 if (EmptyString(ServerInfo.sid))
524 adx 30 {
525 michael 1247 ilog(LOG_TYPE_IRCD, "ERROR: No server id specified in serverinfo block.");
526 adx 30 exit(EXIT_FAILURE);
527     }
528 michael 885
529 michael 1115 strlcpy(me.id, ServerInfo.sid, sizeof(me.id));
530    
531     if (EmptyString(ServerInfo.name))
532     {
533 michael 1247 ilog(LOG_TYPE_IRCD, "ERROR: No server name specified in serverinfo block.");
534 michael 1115 exit(EXIT_FAILURE);
535     }
536    
537 adx 30 strlcpy(me.name, ServerInfo.name, sizeof(me.name));
538    
539     /* serverinfo{} description must exist. If not, error out.*/
540 michael 1115 if (EmptyString(ServerInfo.description))
541 adx 30 {
542 michael 1247 ilog(LOG_TYPE_IRCD, "ERROR: No server description specified in serverinfo block.");
543 adx 30 exit(EXIT_FAILURE);
544     }
545 michael 885
546 adx 30 strlcpy(me.info, ServerInfo.description, sizeof(me.info));
547    
548 michael 1241 me.from = &me;
549     me.servptr = &me;
550     me.localClient->lasttime = CurrentTime;
551     me.localClient->since = CurrentTime;
552     me.localClient->firsttime = CurrentTime;
553 adx 30
554     SetMe(&me);
555     make_server(&me);
556    
557 michael 1115 hash_add_id(&me);
558 adx 30 hash_add_client(&me);
559 michael 2916
560 adx 30 /* add ourselves to global_serv_list */
561     dlinkAdd(&me, make_dlink_node(), &global_serv_list);
562    
563 michael 1622 load_kline_database();
564     load_dline_database();
565     load_gline_database();
566     load_xline_database();
567     load_resv_database();
568    
569 adx 30 if (chdir(MODPATH))
570     {
571 michael 1247 ilog(LOG_TYPE_IRCD, "Could not load core modules. Terminating!");
572 adx 30 exit(EXIT_FAILURE);
573     }
574    
575     load_all_modules(1);
576     load_conf_modules();
577     load_core_modules(1);
578 michael 1115
579 adx 30 /* Go back to DPATH after checking to see if we can chdir to MODPATH */
580 michael 1115 if (chdir(ConfigFileEntry.dpath))
581     {
582     perror("chdir");
583     exit(EXIT_FAILURE);
584     }
585 michael 1121
586 adx 30 /*
587     * assemble_umode_buffer() has to be called after
588     * reading conf/loading modules.
589     */
590     assemble_umode_buffer();
591    
592     write_pidfile(pidFileName);
593    
594 michael 1247 ilog(LOG_TYPE_IRCD, "Server Ready");
595 adx 30
596     eventAddIsh("cleanup_glines", cleanup_glines, NULL, CLEANUP_GLINES_TIME);
597     eventAddIsh("cleanup_tklines", cleanup_tklines, NULL, CLEANUP_TKLINES_TIME);
598    
599     /* We want try_connections to be called as soon as possible now! -- adrian */
600     /* No, 'cause after a restart it would cause all sorts of nick collides */
601     eventAddIsh("try_connections", try_connections, NULL, STARTUP_CONNECTIONS_TIME);
602    
603     /* Setup the timeout check. I'll shift it later :) -- adrian */
604     eventAddIsh("comm_checktimeouts", comm_checktimeouts, NULL, 1);
605    
606 michael 1625 eventAddIsh("save_all_databases", save_all_databases, NULL, DATABASE_UPDATE_TIMEOUT);
607    
608 adx 30 if (ConfigServerHide.links_delay > 0)
609     eventAddIsh("write_links_file", write_links_file, NULL, ConfigServerHide.links_delay);
610     else
611     ConfigServerHide.links_disabled = 1;
612    
613     if (splitmode)
614     eventAddIsh("check_splitmode", check_splitmode, NULL, 60);
615    
616     io_loop();
617 michael 885 return 0;
618 adx 30 }

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision