ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/ircd.c
Revision: 1302
Committed: Wed Mar 21 17:48:54 2012 UTC (13 years, 5 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid-8/src/ircd.c
File size: 17159 byte(s)
Log Message:
- remove servlink in preparation for tls links/compression

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