ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/ircd.c
Revision: 7105
Committed: Sat Jan 23 20:11:27 2016 UTC (9 years, 7 months ago) by michael
Content type: text/x-csrc
File size: 13426 byte(s)
Log Message:
- Incorporate gnutls support by Adam & Attila

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 7006 * Copyright (c) 1997-2016 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 6481 #include "server.h"
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 6735 static int printVersion;
84    
85     static struct lgetopt myopts[] =
86     {
87     { "configfile", &ConfigGeneral.configfile,
88     STRING, "File to use for ircd.conf" },
89     { "klinefile", &ConfigGeneral.klinefile,
90     STRING, "File to use for kline database" },
91     { "dlinefile", &ConfigGeneral.dlinefile,
92     STRING, "File to use for dline database" },
93     { "xlinefile", &ConfigGeneral.xlinefile,
94     STRING, "File to use for xline database" },
95     { "resvfile", &ConfigGeneral.resvfile,
96     STRING, "File to use for resv database" },
97     { "logfile", &logFileName,
98     STRING, "File to use for ircd.log" },
99     { "pidfile", &pidFileName,
100     STRING, "File to use for process ID" },
101     { "foreground", &server_state.foreground,
102     YESNO, "Run in foreground (don't detach)" },
103     { "version", &printVersion,
104     YESNO, "Print version and exit" },
105     { "help", NULL, USAGE, "Print this text" },
106     { NULL, NULL, STRING, NULL },
107     };
108    
109 michael 4094 static struct event event_cleanup_tklines =
110     {
111     .name = "cleanup_tklines",
112     .handler = cleanup_tklines,
113     .when = CLEANUP_TKLINES_TIME
114     };
115    
116     static struct event event_try_connections =
117     {
118     .name = "try_connections",
119     .handler = try_connections,
120     .when = STARTUP_CONNECTIONS_TIME
121     };
122    
123     static struct event event_comm_checktimeouts =
124     {
125     .name = "comm_checktimeouts",
126     .handler = comm_checktimeouts,
127     .when = 1
128     };
129    
130     static struct event event_save_all_databases =
131     {
132     .name = "save_all_databases",
133     .handler = save_all_databases,
134     .when = DATABASE_UPDATE_TIMEOUT
135     };
136    
137     struct event event_write_links_file =
138     {
139     .name = "write_links_file",
140     .handler = write_links_file,
141     };
142    
143    
144 adx 30 void
145     set_time(void)
146     {
147 michael 2978 struct timeval newtime = { .tv_sec = 0, .tv_usec = 0 };
148 adx 30
149     if (gettimeofday(&newtime, NULL) == -1)
150     {
151 michael 6482 char buf[IRCD_BUFSIZE];
152    
153     snprintf(buf, sizeof(buf), "Clock failure, TS can be corrupted: %s",
154     strerror(errno));
155     server_die(buf, SERVER_SHUTDOWN);
156 adx 30 }
157    
158     if (newtime.tv_sec < CurrentTime)
159     {
160 michael 6782 ilog(LOG_TYPE_IRCD, "System clock is running backwards - (%ju < %ju)",
161     newtime.tv_sec, CurrentTime);
162 michael 2980 sendto_realops_flags(UMODE_DEBUG, L_ALL, SEND_NOTICE,
163 michael 6782 "System clock is running backwards - (%ju < %ju)",
164 michael 6802 newtime.tv_sec, CurrentTime);
165 michael 6370 event_set_back_events(CurrentTime - newtime.tv_sec);
166 adx 30 }
167    
168 michael 6481 SystemTime.tv_sec = newtime.tv_sec;
169 adx 30 SystemTime.tv_usec = newtime.tv_usec;
170     }
171    
172     static void
173     io_loop(void)
174     {
175 michael 3215 while (1)
176 adx 30 {
177     if (listing_client_list.head)
178     {
179 michael 4815 dlink_node *node = NULL, *node_next = NULL;
180     DLINK_FOREACH_SAFE(node, node_next, listing_client_list.head)
181     safe_list_channels(node->data, 0);
182 adx 30 }
183    
184 michael 4094 /* Run pending events */
185     event_run();
186 adx 30
187     comm_select();
188     exit_aborted_clients();
189     free_exited_clients();
190    
191 michael 6735 /* Check to see whether we have to rehash the configuration. */
192 adx 30 if (dorehash)
193     {
194 michael 4982 conf_rehash(1);
195 adx 30 dorehash = 0;
196     }
197 michael 3215
198 adx 30 if (doremotd)
199     {
200 michael 2150 motd_recache();
201 michael 6318 sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
202 michael 3065 "Got signal SIGUSR1, reloading motd file(s)");
203 adx 30 doremotd = 0;
204     }
205     }
206     }
207    
208     /* initalialize_global_set_options()
209     *
210     * inputs - none
211     * output - none
212 michael 2916 * side effects - This sets all global set options needed
213 adx 30 */
214     static void
215     initialize_global_set_options(void)
216     {
217 michael 5489 GlobalSetOptions.maxclients = ConfigServerInfo.default_max_clients;
218 michael 5499 GlobalSetOptions.autoconn = 1;
219 adx 30 GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME;
220 michael 5499 GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT;
221     GlobalSetOptions.floodcount = ConfigGeneral.default_floodcount;
222 michael 5489 GlobalSetOptions.joinfloodcount = ConfigChannel.default_join_flood_count;
223     GlobalSetOptions.joinfloodtime = ConfigChannel.default_join_flood_time;
224 adx 30 GlobalSetOptions.ident_timeout = IDENT_TIMEOUT;
225     }
226    
227     /* initialize_server_capabs()
228     *
229     * inputs - none
230     * output - none
231     */
232     static void
233     initialize_server_capabs(void)
234     {
235 michael 6354 add_capability("QS", CAPAB_QS);
236     add_capability("EOB", CAPAB_EOB);
237     add_capability("CLUSTER", CAPAB_CLUSTER);
238     add_capability("SVS", CAPAB_SVS);
239     add_capability("CHW", CAPAB_CHW);
240     add_capability("HOPS", CAPAB_HOPS);
241 adx 30 }
242    
243     /* write_pidfile()
244     *
245     * inputs - filename+path of pid file
246     * output - NONE
247     * side effects - write the pid of the ircd to filename
248     */
249     static void
250     write_pidfile(const char *filename)
251     {
252 michael 1325 FILE *fb;
253 adx 30
254 michael 1325 if ((fb = fopen(filename, "w")))
255 adx 30 {
256 michael 6470 char buf[IRCD_BUFSIZE];
257 adx 30 unsigned int pid = (unsigned int)getpid();
258    
259 michael 6470 snprintf(buf, sizeof(buf), "%u\n", pid);
260 michael 1325
261 michael 6470 if (fputs(buf, fb) == -1)
262 michael 5737 ilog(LOG_TYPE_IRCD, "Error writing to pid file %s: %s",
263     filename, strerror(errno));
264 adx 30
265 michael 1325 fclose(fb);
266 adx 30 }
267     else
268 michael 5566 ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s",
269 michael 4748 filename, strerror(errno));
270 adx 30 }
271    
272     /* check_pidfile()
273     *
274     * inputs - filename+path of pid file
275     * output - none
276     * side effects - reads pid from pidfile and checks if ircd is in process
277     * list. if it is, gracefully exits
278     * -kre
279     */
280     static void
281     check_pidfile(const char *filename)
282     {
283 michael 1325 FILE *fb;
284 michael 6470 char buf[IRCD_BUFSIZE];
285 adx 30
286 michael 1325 if ((fb = fopen(filename, "r")))
287 adx 30 {
288 michael 6470 if (!fgets(buf, 20, fb))
289 michael 6260 ilog(LOG_TYPE_IRCD, "Error reading from pid file %s: %s",
290     filename, strerror(errno));
291 adx 30 else
292     {
293 michael 6481 pid_t pid = atoi(buf);
294 adx 30
295 michael 6481 if (!kill(pid, 0))
296 adx 30 {
297     /* log(L_ERROR, "Server is already running"); */
298     printf("ircd: daemon is already running\n");
299 michael 6646 exit(EXIT_FAILURE);
300 adx 30 }
301     }
302    
303 michael 1325 fclose(fb);
304 adx 30 }
305     else if (errno != ENOENT)
306 michael 6260 ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s",
307     filename, strerror(errno));
308 adx 30 }
309    
310     /* setup_corefile()
311     *
312     * inputs - nothing
313     * output - nothing
314     * side effects - setups corefile to system limits.
315     * -kre
316     */
317     static void
318     setup_corefile(void)
319     {
320     #ifdef HAVE_SYS_RESOURCE_H
321     struct rlimit rlim; /* resource limits */
322    
323     /* Set corefilesize to maximum */
324     if (!getrlimit(RLIMIT_CORE, &rlim))
325     {
326     rlim.rlim_cur = rlim.rlim_max;
327     setrlimit(RLIMIT_CORE, &rlim);
328     }
329     #endif
330     }
331    
332 michael 6735 /*
333     * print_startup - print startup information
334     */
335     static void
336     print_startup(int pid)
337     {
338     printf("ircd: version %s(%s)\n", ircd_version, serno);
339     printf("ircd: pid %d\n", pid);
340     printf("ircd: running in %s mode from %s\n", !server_state.foreground ? "background"
341     : "foreground", ConfigGeneral.dpath);
342     }
343    
344     static void
345     make_daemon(void)
346     {
347     int pid;
348    
349     if ((pid = fork()) < 0)
350     {
351     perror("fork");
352     exit(EXIT_FAILURE);
353     }
354     else if (pid > 0)
355     {
356     print_startup(pid);
357     exit(EXIT_SUCCESS);
358     }
359    
360     setsid();
361     }
362    
363 adx 30 int
364     main(int argc, char *argv[])
365     {
366 michael 2253 /* Check to see if the user is running us as root, which is a nono */
367 michael 4733 if (!geteuid())
368 adx 30 {
369 michael 3525 fprintf(stderr, "ERROR: This server won't run as root/superuser\n");
370 michael 982 return -1;
371 adx 30 }
372    
373     /* Setup corefile size immediately after boot -kre */
374     setup_corefile();
375    
376 michael 5545 /* Save server boot time right away, so getrusage works correctly */
377 adx 30 set_time();
378    
379 michael 5545 /* It's not random, but it ought to be a little harder to guess */
380 michael 982 init_genrand(SystemTime.tv_sec ^ (SystemTime.tv_usec | (getpid() << 20)));
381    
382 michael 4340 ConfigGeneral.dpath = DPATH;
383     ConfigGeneral.spath = SPATH;
384     ConfigGeneral.mpath = MPATH;
385     ConfigGeneral.configfile = CPATH; /* Server configuration file */
386     ConfigGeneral.klinefile = KPATH; /* Server kline file */
387     ConfigGeneral.xlinefile = XPATH; /* Server xline file */
388     ConfigGeneral.dlinefile = DLPATH; /* dline file */
389     ConfigGeneral.resvfile = RESVPATH; /* resv file */
390 michael 1702
391 adx 30 myargv = argv;
392 michael 5723 umask(077); /* umask 077: u=rwx,g=,o= */
393 adx 30
394     parseargs(&argc, &argv, myopts);
395    
396     if (printVersion)
397     {
398 michael 2646 printf("ircd: version %s(%s)\n", ircd_version, serno);
399 adx 30 exit(EXIT_SUCCESS);
400     }
401    
402 michael 4340 if (chdir(ConfigGeneral.dpath))
403 adx 30 {
404     perror("chdir");
405     exit(EXIT_FAILURE);
406     }
407    
408     if (!server_state.foreground)
409     {
410     make_daemon();
411 michael 7105 //https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=768841 says close_standard_fds should be ok
412     //since we are initing gnutls explicitly on the other side, but it doesn't work for me
413     //close_standard_fds(); /* this needs to be before init_netio()! */
414 adx 30 }
415     else
416     print_startup(getpid());
417    
418 michael 7105 tls_init();
419 adx 30 setup_signals();
420    
421     /* We need this to initialise the fd array before anything else */
422     fdlist_init();
423 michael 1831 log_set_file(LOG_TYPE_IRCD, 0, logFileName);
424 michael 4415
425 michael 2632 init_netio(); /* This needs to be setup early ! -- adrian */
426 michael 2253
427 adx 30 /* Check if there is pidfile and daemon already running */
428     check_pidfile(pidFileName);
429    
430 michael 1654 mp_pool_init();
431 adx 30 init_dlink_nodes();
432 michael 6185 isupport_init();
433 adx 30 dbuf_init();
434 michael 1798 hash_init();
435 michael 6393 userhost_init();
436 michael 4319 ipcache_init();
437 michael 1798 client_init();
438 michael 1632 class_init();
439 michael 1358 whowas_init();
440 michael 876 watch_init();
441 michael 1798 auth_init(); /* Initialise the auth code */
442 michael 998 init_resolver(); /* Needs to be setup before the io loop */
443 michael 1404 modules_init();
444 adx 30 read_conf_files(1); /* cold start init conf files */
445     initialize_server_capabs(); /* Set up default_server_capabs */
446 michael 5489 initialize_global_set_options(); /* Has to be called after read_conf_files() */
447 michael 1798 channel_init();
448 michael 2216 read_links_file();
449 michael 2150 motd_init();
450 michael 6189 user_modes_init();
451 michael 1858 #ifdef HAVE_LIBGEOIP
452     geoip_ctx = GeoIP_new(GEOIP_MEMORY_CACHE);
453     #endif
454 adx 30
455 michael 4340 if (EmptyString(ConfigServerInfo.name))
456 michael 1115 {
457 michael 1247 ilog(LOG_TYPE_IRCD, "ERROR: No server name specified in serverinfo block.");
458 michael 1115 exit(EXIT_FAILURE);
459     }
460    
461 michael 4340 strlcpy(me.name, ConfigServerInfo.name, sizeof(me.name));
462 adx 30
463 michael 6481 /* serverinfo {} description must exist. If not, error out.*/
464 michael 4340 if (EmptyString(ConfigServerInfo.description))
465 adx 30 {
466 michael 1247 ilog(LOG_TYPE_IRCD, "ERROR: No server description specified in serverinfo block.");
467 adx 30 exit(EXIT_FAILURE);
468     }
469 michael 885
470 michael 4340 strlcpy(me.info, ConfigServerInfo.description, sizeof(me.info));
471 adx 30
472 michael 6156 if (EmptyString(ConfigServerInfo.sid))
473     {
474     ilog(LOG_TYPE_IRCD, "Generating server ID");
475     generate_sid();
476     }
477     else
478     strlcpy(me.id, ConfigServerInfo.sid, sizeof(me.id));
479    
480 michael 6464 init_uid();
481    
482 michael 5545 me.from = &me;
483     me.servptr = &me;
484     me.connection->lasttime = CurrentTime;
485     me.connection->since = CurrentTime;
486 michael 4588 me.connection->firsttime = CurrentTime;
487 adx 30
488     SetMe(&me);
489     make_server(&me);
490    
491 michael 1115 hash_add_id(&me);
492 adx 30 hash_add_client(&me);
493 michael 2916
494 michael 4209 dlinkAdd(&me, make_dlink_node(), &global_server_list);
495 michael 6464 dlinkAdd(&me, &me.node, &global_client_list);
496 adx 30
497 michael 6928 load_kline_database(ConfigGeneral.klinefile);
498     load_dline_database(ConfigGeneral.dlinefile);
499     load_xline_database(ConfigGeneral.xlinefile);
500     load_resv_database(ConfigGeneral.resvfile);
501 michael 1622
502 adx 30 load_all_modules(1);
503     load_conf_modules();
504     load_core_modules(1);
505 michael 1115
506 adx 30 write_pidfile(pidFileName);
507    
508 michael 4094 event_addish(&event_cleanup_tklines, NULL);
509 adx 30
510     /* We want try_connections to be called as soon as possible now! -- adrian */
511     /* No, 'cause after a restart it would cause all sorts of nick collides */
512 michael 4094 event_addish(&event_try_connections, NULL);
513 adx 30
514     /* Setup the timeout check. I'll shift it later :) -- adrian */
515 michael 4399 event_add(&event_comm_checktimeouts, NULL);
516 adx 30
517 michael 4094 event_addish(&event_save_all_databases, NULL);
518 michael 1625
519 michael 6642 if (ConfigServerHide.flatten_links_delay && event_write_links_file.active == 0)
520 michael 4094 {
521 michael 6597 event_write_links_file.when = ConfigServerHide.flatten_links_delay;
522 michael 6636 event_add(&event_write_links_file, NULL);
523 michael 4094 }
524 adx 30
525 michael 6464 ilog(LOG_TYPE_IRCD, "Server Ready");
526 adx 30 io_loop();
527 michael 6464
528 michael 885 return 0;
529 adx 30 }

Properties

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