ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/ircd.c
Revision: 2156
Committed: Fri May 31 20:53:11 2013 UTC (12 years, 2 months ago) by michael
Content type: text/x-csrc
File size: 17304 byte(s)
Log Message:
- Cleanup flattened links code

File Contents

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

Properties

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