ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/ircd.c
Revision: 2872
Committed: Sun Jan 19 17:25:38 2014 UTC (11 years, 7 months ago) by michael
Content type: text/x-csrc
File size: 17084 byte(s)
Log Message:
- Moved configuration related code from ircd.c to conf.c

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

Properties

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