ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/ircd.c
Revision: 1117
Committed: Tue Dec 21 15:01:46 2010 UTC (13 years, 3 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid-7.3/src/ircd.c
File size: 17693 byte(s)
Log Message:
- ircd_parser.y: ensure only valid hostnames can be
  specified in serverinfo::name

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

Properties

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