ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/ircd.c
Revision: 6393
Committed: Sun Aug 23 14:58:44 2015 UTC (10 years ago) by michael
Content type: text/x-csrc
File size: 15807 byte(s)
Log Message:
- Move userhost related code from hash.c to userhost.c

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 5347 * Copyright (c) 1997-2015 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 michael 4565 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 adx 30 * 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 michael 6161 #include "id.h"
37 adx 30 #include "irc_string.h"
38     #include "ircd_signal.h"
39     #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 michael 876 #include "watch.h"
57 michael 1622 #include "conf_db.h"
58 michael 1632 #include "conf_class.h"
59 michael 4325 #include "ipcache.h"
60 michael 6185 #include "isupport.h"
61 michael 6393 #include "userhost.h"
62 adx 30
63 michael 1858
64     #ifdef HAVE_LIBGEOIP
65     GeoIP *geoip_ctx;
66     #endif
67 michael 2872
68 michael 5737 struct SetOptions GlobalSetOptions; /* /quote set variables */
69 michael 5602 struct Counter Count;
70     struct ServerState_t server_state;
71     struct ServerStatistics ServerStats;
72 adx 30 struct timeval SystemTime;
73 michael 5737 struct Connection meConnection; /* That's also part of me */
74 michael 5470 struct Client me = { .connection = &meConnection }; /* That's me */
75 adx 30
76 michael 5460 char **myargv;
77 adx 30 const char *logFileName = LPATH;
78     const char *pidFileName = PPATH;
79    
80 michael 5460 unsigned int dorehash;
81     unsigned int doremotd;
82 adx 30
83 michael 4094 static struct event event_cleanup_tklines =
84     {
85     .name = "cleanup_tklines",
86     .handler = cleanup_tklines,
87     .when = CLEANUP_TKLINES_TIME
88     };
89    
90     static struct event event_try_connections =
91     {
92     .name = "try_connections",
93     .handler = try_connections,
94     .when = STARTUP_CONNECTIONS_TIME
95     };
96    
97     static struct event event_comm_checktimeouts =
98     {
99     .name = "comm_checktimeouts",
100     .handler = comm_checktimeouts,
101     .when = 1
102     };
103    
104     static struct event event_save_all_databases =
105     {
106     .name = "save_all_databases",
107     .handler = save_all_databases,
108     .when = DATABASE_UPDATE_TIMEOUT
109     };
110    
111     struct event event_write_links_file =
112     {
113     .name = "write_links_file",
114     .handler = write_links_file,
115     };
116    
117    
118 adx 30 /*
119     * print_startup - print startup information
120     */
121     static void
122     print_startup(int pid)
123     {
124 michael 2646 printf("ircd: version %s(%s)\n", ircd_version, serno);
125 adx 30 printf("ircd: pid %d\n", pid);
126     printf("ircd: running in %s mode from %s\n", !server_state.foreground ? "background"
127 michael 4340 : "foreground", ConfigGeneral.dpath);
128 adx 30 }
129    
130     static void
131     make_daemon(void)
132     {
133     int pid;
134    
135     if ((pid = fork()) < 0)
136     {
137     perror("fork");
138     exit(EXIT_FAILURE);
139     }
140     else if (pid > 0)
141     {
142     print_startup(pid);
143     exit(EXIT_SUCCESS);
144     }
145    
146     setsid();
147     }
148    
149     static int printVersion = 0;
150    
151 michael 3215 static struct lgetopt myopts[] =
152     {
153 michael 4340 {"configfile", &ConfigGeneral.configfile,
154 adx 30 STRING, "File to use for ircd.conf"},
155 michael 4340 {"klinefile", &ConfigGeneral.klinefile,
156 michael 1718 STRING, "File to use for kline database"},
157 michael 4340 {"dlinefile", &ConfigGeneral.dlinefile,
158 michael 1718 STRING, "File to use for dline database"},
159 michael 4340 {"xlinefile", &ConfigGeneral.xlinefile,
160 michael 1718 STRING, "File to use for xline database"},
161 michael 4340 {"resvfile", &ConfigGeneral.resvfile,
162 michael 1718 STRING, "File to use for resv database"},
163 michael 2916 {"logfile", &logFileName,
164 adx 30 STRING, "File to use for ircd.log"},
165     {"pidfile", &pidFileName,
166     STRING, "File to use for process ID"},
167 michael 2916 {"foreground", &server_state.foreground,
168 adx 30 YESNO, "Run in foreground (don't detach)"},
169 michael 2916 {"version", &printVersion,
170 adx 30 YESNO, "Print version and exit"},
171     {"help", NULL, USAGE, "Print this text"},
172     {NULL, NULL, STRING, NULL},
173     };
174    
175     void
176     set_time(void)
177     {
178 michael 2978 struct timeval newtime = { .tv_sec = 0, .tv_usec = 0 };
179 adx 30
180     if (gettimeofday(&newtime, NULL) == -1)
181     {
182 michael 1247 ilog(LOG_TYPE_IRCD, "Clock Failure (%s), TS can be corrupted",
183 adx 30 strerror(errno));
184 michael 6318 sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
185 adx 30 "Clock Failure (%s), TS can be corrupted",
186     strerror(errno));
187 michael 6357 server_die("Clock Failure", SERVER_SHUTDOWN);
188 adx 30 }
189    
190     if (newtime.tv_sec < CurrentTime)
191     {
192 michael 2980 ilog(LOG_TYPE_IRCD, "System clock is running backwards - (%lu < %lu)",
193     (unsigned long)newtime.tv_sec, (unsigned long)CurrentTime);
194     sendto_realops_flags(UMODE_DEBUG, L_ALL, SEND_NOTICE,
195     "System clock is running backwards - (%lu < %lu)",
196     (unsigned long)newtime.tv_sec,
197     (unsigned long)CurrentTime);
198 michael 6370 event_set_back_events(CurrentTime - newtime.tv_sec);
199 adx 30 }
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 michael 3215 while (1)
209 adx 30 {
210     if (listing_client_list.head)
211     {
212 michael 4815 dlink_node *node = NULL, *node_next = NULL;
213     DLINK_FOREACH_SAFE(node, node_next, listing_client_list.head)
214     safe_list_channels(node->data, 0);
215 adx 30 }
216    
217 michael 4094 /* Run pending events */
218     event_run();
219 adx 30
220     comm_select();
221     exit_aborted_clients();
222     free_exited_clients();
223    
224     /* Check to see whether we have to rehash the configuration .. */
225     if (dorehash)
226     {
227 michael 4982 conf_rehash(1);
228 adx 30 dorehash = 0;
229     }
230 michael 3215
231 adx 30 if (doremotd)
232     {
233 michael 2150 motd_recache();
234 michael 6318 sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
235 michael 3065 "Got signal SIGUSR1, reloading motd file(s)");
236 adx 30 doremotd = 0;
237     }
238     }
239     }
240    
241     /* initalialize_global_set_options()
242     *
243     * inputs - none
244     * output - none
245 michael 2916 * side effects - This sets all global set options needed
246 adx 30 */
247     static void
248     initialize_global_set_options(void)
249     {
250 michael 5489 GlobalSetOptions.maxclients = ConfigServerInfo.default_max_clients;
251 michael 5499 GlobalSetOptions.autoconn = 1;
252 adx 30 GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME;
253 michael 5499 GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT;
254     GlobalSetOptions.floodcount = ConfigGeneral.default_floodcount;
255 michael 5489 GlobalSetOptions.joinfloodcount = ConfigChannel.default_join_flood_count;
256     GlobalSetOptions.joinfloodtime = ConfigChannel.default_join_flood_time;
257 adx 30 GlobalSetOptions.ident_timeout = IDENT_TIMEOUT;
258     }
259    
260     /* initialize_server_capabs()
261     *
262     * inputs - none
263     * output - none
264     */
265     static void
266     initialize_server_capabs(void)
267     {
268 michael 6354 add_capability("QS", CAPAB_QS);
269     add_capability("EOB", CAPAB_EOB);
270     add_capability("CLUSTER", CAPAB_CLUSTER);
271     add_capability("SVS", CAPAB_SVS);
272     add_capability("CHW", CAPAB_CHW);
273     add_capability("HOPS", CAPAB_HOPS);
274 adx 30 }
275    
276     /* write_pidfile()
277     *
278     * inputs - filename+path of pid file
279     * output - NONE
280     * side effects - write the pid of the ircd to filename
281     */
282     static void
283     write_pidfile(const char *filename)
284     {
285 michael 1325 FILE *fb;
286 adx 30
287 michael 1325 if ((fb = fopen(filename, "w")))
288 adx 30 {
289 michael 2691 char buff[IRCD_BUFSIZE];
290 adx 30 unsigned int pid = (unsigned int)getpid();
291    
292 michael 1325 snprintf(buff, sizeof(buff), "%u\n", pid);
293    
294 michael 4736 if (fputs(buff, fb) == -1)
295 michael 5737 ilog(LOG_TYPE_IRCD, "Error writing to pid file %s: %s",
296     filename, strerror(errno));
297 adx 30
298 michael 1325 fclose(fb);
299 adx 30 }
300     else
301 michael 5566 ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s",
302 michael 4748 filename, strerror(errno));
303 adx 30 }
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 michael 1325 if ((fb = fopen(filename, "r")))
321 adx 30 {
322 michael 4733 if (!fgets(buff, 20, fb))
323 michael 6260 ilog(LOG_TYPE_IRCD, "Error reading from pid file %s: %s",
324     filename, strerror(errno));
325 adx 30 else
326     {
327     pidfromfile = atoi(buff);
328    
329     if (!kill(pidfromfile, 0))
330     {
331     /* log(L_ERROR, "Server is already running"); */
332     printf("ircd: daemon is already running\n");
333     exit(-1);
334     }
335     }
336    
337 michael 1325 fclose(fb);
338 adx 30 }
339     else if (errno != ENOENT)
340 michael 6260 ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s",
341     filename, strerror(errno));
342 adx 30 }
343    
344     /* setup_corefile()
345     *
346     * inputs - nothing
347     * output - nothing
348     * side effects - setups corefile to system limits.
349     * -kre
350     */
351     static void
352     setup_corefile(void)
353     {
354     #ifdef HAVE_SYS_RESOURCE_H
355     struct rlimit rlim; /* resource limits */
356    
357     /* Set corefilesize to maximum */
358     if (!getrlimit(RLIMIT_CORE, &rlim))
359     {
360     rlim.rlim_cur = rlim.rlim_max;
361     setrlimit(RLIMIT_CORE, &rlim);
362     }
363     #endif
364     }
365    
366 michael 2228 #ifdef HAVE_LIBCRYPTO
367     static int
368     always_accept_verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx)
369     {
370     return 1;
371     }
372     #endif
373    
374 adx 30 /* init_ssl()
375     *
376     * inputs - nothing
377     * output - nothing
378     * side effects - setups SSL context.
379     */
380     static void
381 michael 1798 ssl_init(void)
382 adx 30 {
383     #ifdef HAVE_LIBCRYPTO
384     SSL_load_error_strings();
385     SSLeay_add_ssl_algorithms();
386    
387 michael 4733 if (!(ConfigServerInfo.server_ctx = SSL_CTX_new(SSLv23_server_method())))
388 adx 30 {
389 michael 4129 const char *s = ERR_lib_error_string(ERR_get_error());
390 adx 30
391 michael 4129 fprintf(stderr, "ERROR: Could not initialize the SSL Server context -- %s\n", s);
392 michael 4763 ilog(LOG_TYPE_IRCD, "ERROR: Could not initialize the SSL Server context -- %s", s);
393 michael 4497 exit(EXIT_FAILURE);
394 michael 4751 return; /* Not reached */
395 adx 30 }
396    
397 michael 4730 SSL_CTX_set_options(ConfigServerInfo.server_ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TICKET);
398 michael 4593 SSL_CTX_set_options(ConfigServerInfo.server_ctx, SSL_OP_SINGLE_DH_USE|SSL_OP_CIPHER_SERVER_PREFERENCE);
399 michael 4340 SSL_CTX_set_verify(ConfigServerInfo.server_ctx, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,
400 michael 2228 always_accept_verify_cb);
401 michael 4730 SSL_CTX_set_session_cache_mode(ConfigServerInfo.server_ctx, SSL_SESS_CACHE_OFF);
402 michael 4892 SSL_CTX_set_cipher_list(ConfigServerInfo.server_ctx, "EECDH+HIGH:EDH+HIGH:HIGH:!aNULL");
403 adx 30
404 michael 4744 #if OPENSSL_VERSION_NUMBER >= 0x009080FFL && !defined(OPENSSL_NO_ECDH)
405 michael 4070 {
406     EC_KEY *key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
407    
408     if (key)
409     {
410 michael 4340 SSL_CTX_set_tmp_ecdh(ConfigServerInfo.server_ctx, key);
411 michael 4070 EC_KEY_free(key);
412     }
413     }
414    
415 michael 4340 SSL_CTX_set_options(ConfigServerInfo.server_ctx, SSL_OP_SINGLE_ECDH_USE);
416 michael 4070 #endif
417    
418 michael 4733 if (!(ConfigServerInfo.client_ctx = SSL_CTX_new(SSLv23_client_method())))
419 michael 1303 {
420 michael 4129 const char *s = ERR_lib_error_string(ERR_get_error());
421 michael 1303
422 michael 4129 fprintf(stderr, "ERROR: Could not initialize the SSL Client context -- %s\n", s);
423 michael 4763 ilog(LOG_TYPE_IRCD, "ERROR: Could not initialize the SSL Client context -- %s", s);
424 michael 4497 exit(EXIT_FAILURE);
425 michael 4751 return; /* Not reached */
426 michael 1303 }
427    
428 michael 4730 SSL_CTX_set_options(ConfigServerInfo.client_ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TICKET);
429 michael 4340 SSL_CTX_set_options(ConfigServerInfo.client_ctx, SSL_OP_SINGLE_DH_USE);
430     SSL_CTX_set_verify(ConfigServerInfo.client_ctx, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,
431 michael 2228 always_accept_verify_cb);
432 michael 4730 SSL_CTX_set_session_cache_mode(ConfigServerInfo.client_ctx, SSL_SESS_CACHE_OFF);
433 adx 30 #endif /* HAVE_LIBCRYPTO */
434     }
435    
436     int
437     main(int argc, char *argv[])
438     {
439 michael 2253 /* Check to see if the user is running us as root, which is a nono */
440 michael 4733 if (!geteuid())
441 adx 30 {
442 michael 3525 fprintf(stderr, "ERROR: This server won't run as root/superuser\n");
443 michael 982 return -1;
444 adx 30 }
445    
446     /* Setup corefile size immediately after boot -kre */
447     setup_corefile();
448    
449 michael 5545 /* Save server boot time right away, so getrusage works correctly */
450 adx 30 set_time();
451    
452 michael 5545 /* It's not random, but it ought to be a little harder to guess */
453 michael 982 init_genrand(SystemTime.tv_sec ^ (SystemTime.tv_usec | (getpid() << 20)));
454    
455 michael 5545 dlinkAdd(&me, &me.node, &global_client_list);
456    
457 michael 4340 ConfigGeneral.dpath = DPATH;
458     ConfigGeneral.spath = SPATH;
459     ConfigGeneral.mpath = MPATH;
460     ConfigGeneral.configfile = CPATH; /* Server configuration file */
461     ConfigGeneral.klinefile = KPATH; /* Server kline file */
462     ConfigGeneral.xlinefile = XPATH; /* Server xline file */
463     ConfigGeneral.dlinefile = DLPATH; /* dline file */
464     ConfigGeneral.resvfile = RESVPATH; /* resv file */
465 michael 1702
466 adx 30 myargv = argv;
467 michael 5723 umask(077); /* umask 077: u=rwx,g=,o= */
468 adx 30
469     parseargs(&argc, &argv, myopts);
470    
471     if (printVersion)
472     {
473 michael 2646 printf("ircd: version %s(%s)\n", ircd_version, serno);
474 adx 30 exit(EXIT_SUCCESS);
475     }
476    
477 michael 4340 if (chdir(ConfigGeneral.dpath))
478 adx 30 {
479     perror("chdir");
480     exit(EXIT_FAILURE);
481     }
482    
483 michael 1798 ssl_init();
484 adx 30
485     if (!server_state.foreground)
486     {
487     make_daemon();
488     close_standard_fds(); /* this needs to be before init_netio()! */
489     }
490     else
491     print_startup(getpid());
492    
493     setup_signals();
494    
495     /* We need this to initialise the fd array before anything else */
496     fdlist_init();
497 michael 1831 log_set_file(LOG_TYPE_IRCD, 0, logFileName);
498 michael 4415
499 michael 2632 init_netio(); /* This needs to be setup early ! -- adrian */
500 michael 2253
501 adx 30 /* Check if there is pidfile and daemon already running */
502     check_pidfile(pidFileName);
503    
504 michael 1654 mp_pool_init();
505 adx 30 init_dlink_nodes();
506 michael 6185 isupport_init();
507 adx 30 dbuf_init();
508 michael 1798 hash_init();
509 michael 6393 userhost_init();
510 michael 4319 ipcache_init();
511 michael 1798 client_init();
512 michael 1632 class_init();
513 michael 1358 whowas_init();
514 michael 876 watch_init();
515 michael 1798 auth_init(); /* Initialise the auth code */
516 michael 998 init_resolver(); /* Needs to be setup before the io loop */
517 michael 1404 modules_init();
518 adx 30 read_conf_files(1); /* cold start init conf files */
519     initialize_server_capabs(); /* Set up default_server_capabs */
520 michael 5489 initialize_global_set_options(); /* Has to be called after read_conf_files() */
521 michael 1798 channel_init();
522 michael 2216 read_links_file();
523 michael 2150 motd_init();
524 michael 6189 user_modes_init();
525 michael 1858 #ifdef HAVE_LIBGEOIP
526     geoip_ctx = GeoIP_new(GEOIP_MEMORY_CACHE);
527     #endif
528 adx 30
529 michael 4340 if (EmptyString(ConfigServerInfo.name))
530 michael 1115 {
531 michael 1247 ilog(LOG_TYPE_IRCD, "ERROR: No server name specified in serverinfo block.");
532 michael 1115 exit(EXIT_FAILURE);
533     }
534    
535 michael 4340 strlcpy(me.name, ConfigServerInfo.name, sizeof(me.name));
536 adx 30
537     /* serverinfo{} description must exist. If not, error out.*/
538 michael 4340 if (EmptyString(ConfigServerInfo.description))
539 adx 30 {
540 michael 1247 ilog(LOG_TYPE_IRCD, "ERROR: No server description specified in serverinfo block.");
541 adx 30 exit(EXIT_FAILURE);
542     }
543 michael 885
544 michael 4340 strlcpy(me.info, ConfigServerInfo.description, sizeof(me.info));
545 adx 30
546 michael 6156 if (EmptyString(ConfigServerInfo.sid))
547     {
548     ilog(LOG_TYPE_IRCD, "Generating server ID");
549     generate_sid();
550     }
551     else
552     {
553     strlcpy(me.id, ConfigServerInfo.sid, sizeof(me.id));
554     }
555    
556 michael 5545 me.from = &me;
557     me.servptr = &me;
558     me.connection->lasttime = CurrentTime;
559     me.connection->since = CurrentTime;
560 michael 4588 me.connection->firsttime = CurrentTime;
561 adx 30
562     SetMe(&me);
563     make_server(&me);
564    
565 michael 1115 hash_add_id(&me);
566 adx 30 hash_add_client(&me);
567 michael 2916
568 michael 4209 dlinkAdd(&me, make_dlink_node(), &global_server_list);
569 adx 30
570 michael 6156 init_uid();
571    
572 michael 1622 load_kline_database();
573     load_dline_database();
574     load_xline_database();
575     load_resv_database();
576    
577 adx 30 load_all_modules(1);
578     load_conf_modules();
579     load_core_modules(1);
580 michael 1115
581 adx 30 write_pidfile(pidFileName);
582    
583 michael 1247 ilog(LOG_TYPE_IRCD, "Server Ready");
584 adx 30
585 michael 4094 event_addish(&event_cleanup_tklines, NULL);
586 adx 30
587     /* We want try_connections to be called as soon as possible now! -- adrian */
588     /* No, 'cause after a restart it would cause all sorts of nick collides */
589 michael 4094 event_addish(&event_try_connections, NULL);
590 adx 30
591     /* Setup the timeout check. I'll shift it later :) -- adrian */
592 michael 4399 event_add(&event_comm_checktimeouts, NULL);
593 adx 30
594 michael 4094 event_addish(&event_save_all_databases, NULL);
595 michael 1625
596 adx 30 if (ConfigServerHide.links_delay > 0)
597 michael 4094 {
598     event_write_links_file.when = ConfigServerHide.links_delay;
599     event_addish(&event_write_links_file, NULL);
600     }
601 adx 30 else
602     ConfigServerHide.links_disabled = 1;
603    
604     io_loop();
605 michael 885 return 0;
606 adx 30 }

Properties

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