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

# Content
1 /*
2 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 *
4 * Copyright (c) 1997-2016 ircd-hybrid development team
5 *
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 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 * USA
20 */
21
22 /*! \file ircd.c
23 * \brief Starts up and runs the ircd.
24 * \version $Id$
25 */
26
27 #include "stdinc.h"
28 #include "user.h"
29 #include "list.h"
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 #include "id.h"
37 #include "irc_string.h"
38 #include "ircd_signal.h"
39 #include "motd.h"
40 #include "conf.h"
41 #include "hostmask.h"
42 #include "parse.h"
43 #include "res.h"
44 #include "restart.h"
45 #include "rng_mt.h"
46 #include "auth.h"
47 #include "s_bsd.h"
48 #include "log.h"
49 #include "server.h"
50 #include "send.h"
51 #include "whowas.h"
52 #include "modules.h"
53 #include "memory.h"
54 #include "mempool.h"
55 #include "ircd_getopt.h"
56 #include "watch.h"
57 #include "conf_db.h"
58 #include "conf_class.h"
59 #include "ipcache.h"
60 #include "isupport.h"
61 #include "userhost.h"
62
63
64 #ifdef HAVE_LIBGEOIP
65 GeoIP *geoip_ctx;
66 #endif
67
68 struct SetOptions GlobalSetOptions; /* /quote set variables */
69 struct Counter Count;
70 struct ServerState_t server_state;
71 struct ServerStatistics ServerStats;
72 struct timeval SystemTime;
73 struct Connection meConnection; /* That's also part of me */
74 struct Client me = { .connection = &meConnection }; /* That's me */
75
76 char **myargv;
77 const char *logFileName = LPATH;
78 const char *pidFileName = PPATH;
79
80 unsigned int dorehash;
81 unsigned int doremotd;
82
83 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 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 void
145 set_time(void)
146 {
147 struct timeval newtime = { .tv_sec = 0, .tv_usec = 0 };
148
149 if (gettimeofday(&newtime, NULL) == -1)
150 {
151 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 }
157
158 if (newtime.tv_sec < CurrentTime)
159 {
160 ilog(LOG_TYPE_IRCD, "System clock is running backwards - (%ju < %ju)",
161 newtime.tv_sec, CurrentTime);
162 sendto_realops_flags(UMODE_DEBUG, L_ALL, SEND_NOTICE,
163 "System clock is running backwards - (%ju < %ju)",
164 newtime.tv_sec, CurrentTime);
165 event_set_back_events(CurrentTime - newtime.tv_sec);
166 }
167
168 SystemTime.tv_sec = newtime.tv_sec;
169 SystemTime.tv_usec = newtime.tv_usec;
170 }
171
172 static void
173 io_loop(void)
174 {
175 while (1)
176 {
177 if (listing_client_list.head)
178 {
179 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 }
183
184 /* Run pending events */
185 event_run();
186
187 comm_select();
188 exit_aborted_clients();
189 free_exited_clients();
190
191 /* Check to see whether we have to rehash the configuration. */
192 if (dorehash)
193 {
194 conf_rehash(1);
195 dorehash = 0;
196 }
197
198 if (doremotd)
199 {
200 motd_recache();
201 sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
202 "Got signal SIGUSR1, reloading motd file(s)");
203 doremotd = 0;
204 }
205 }
206 }
207
208 /* initalialize_global_set_options()
209 *
210 * inputs - none
211 * output - none
212 * side effects - This sets all global set options needed
213 */
214 static void
215 initialize_global_set_options(void)
216 {
217 GlobalSetOptions.maxclients = ConfigServerInfo.default_max_clients;
218 GlobalSetOptions.autoconn = 1;
219 GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME;
220 GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT;
221 GlobalSetOptions.floodcount = ConfigGeneral.default_floodcount;
222 GlobalSetOptions.joinfloodcount = ConfigChannel.default_join_flood_count;
223 GlobalSetOptions.joinfloodtime = ConfigChannel.default_join_flood_time;
224 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 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 }
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 FILE *fb;
253
254 if ((fb = fopen(filename, "w")))
255 {
256 char buf[IRCD_BUFSIZE];
257 unsigned int pid = (unsigned int)getpid();
258
259 snprintf(buf, sizeof(buf), "%u\n", pid);
260
261 if (fputs(buf, fb) == -1)
262 ilog(LOG_TYPE_IRCD, "Error writing to pid file %s: %s",
263 filename, strerror(errno));
264
265 fclose(fb);
266 }
267 else
268 ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s",
269 filename, strerror(errno));
270 }
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 FILE *fb;
284 char buf[IRCD_BUFSIZE];
285
286 if ((fb = fopen(filename, "r")))
287 {
288 if (!fgets(buf, 20, fb))
289 ilog(LOG_TYPE_IRCD, "Error reading from pid file %s: %s",
290 filename, strerror(errno));
291 else
292 {
293 pid_t pid = atoi(buf);
294
295 if (!kill(pid, 0))
296 {
297 /* log(L_ERROR, "Server is already running"); */
298 printf("ircd: daemon is already running\n");
299 exit(EXIT_FAILURE);
300 }
301 }
302
303 fclose(fb);
304 }
305 else if (errno != ENOENT)
306 ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s",
307 filename, strerror(errno));
308 }
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 /*
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 int
364 main(int argc, char *argv[])
365 {
366 /* Check to see if the user is running us as root, which is a nono */
367 if (!geteuid())
368 {
369 fprintf(stderr, "ERROR: This server won't run as root/superuser\n");
370 return -1;
371 }
372
373 /* Setup corefile size immediately after boot -kre */
374 setup_corefile();
375
376 /* Save server boot time right away, so getrusage works correctly */
377 set_time();
378
379 /* It's not random, but it ought to be a little harder to guess */
380 init_genrand(SystemTime.tv_sec ^ (SystemTime.tv_usec | (getpid() << 20)));
381
382 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
391 myargv = argv;
392 umask(077); /* umask 077: u=rwx,g=,o= */
393
394 parseargs(&argc, &argv, myopts);
395
396 if (printVersion)
397 {
398 printf("ircd: version %s(%s)\n", ircd_version, serno);
399 exit(EXIT_SUCCESS);
400 }
401
402 if (chdir(ConfigGeneral.dpath))
403 {
404 perror("chdir");
405 exit(EXIT_FAILURE);
406 }
407
408 if (!server_state.foreground)
409 {
410 make_daemon();
411 //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 }
415 else
416 print_startup(getpid());
417
418 tls_init();
419 setup_signals();
420
421 /* We need this to initialise the fd array before anything else */
422 fdlist_init();
423 log_set_file(LOG_TYPE_IRCD, 0, logFileName);
424
425 init_netio(); /* This needs to be setup early ! -- adrian */
426
427 /* Check if there is pidfile and daemon already running */
428 check_pidfile(pidFileName);
429
430 mp_pool_init();
431 init_dlink_nodes();
432 isupport_init();
433 dbuf_init();
434 hash_init();
435 userhost_init();
436 ipcache_init();
437 client_init();
438 class_init();
439 whowas_init();
440 watch_init();
441 auth_init(); /* Initialise the auth code */
442 init_resolver(); /* Needs to be setup before the io loop */
443 modules_init();
444 read_conf_files(1); /* cold start init conf files */
445 initialize_server_capabs(); /* Set up default_server_capabs */
446 initialize_global_set_options(); /* Has to be called after read_conf_files() */
447 channel_init();
448 read_links_file();
449 motd_init();
450 user_modes_init();
451 #ifdef HAVE_LIBGEOIP
452 geoip_ctx = GeoIP_new(GEOIP_MEMORY_CACHE);
453 #endif
454
455 if (EmptyString(ConfigServerInfo.name))
456 {
457 ilog(LOG_TYPE_IRCD, "ERROR: No server name specified in serverinfo block.");
458 exit(EXIT_FAILURE);
459 }
460
461 strlcpy(me.name, ConfigServerInfo.name, sizeof(me.name));
462
463 /* serverinfo {} description must exist. If not, error out.*/
464 if (EmptyString(ConfigServerInfo.description))
465 {
466 ilog(LOG_TYPE_IRCD, "ERROR: No server description specified in serverinfo block.");
467 exit(EXIT_FAILURE);
468 }
469
470 strlcpy(me.info, ConfigServerInfo.description, sizeof(me.info));
471
472 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 init_uid();
481
482 me.from = &me;
483 me.servptr = &me;
484 me.connection->lasttime = CurrentTime;
485 me.connection->since = CurrentTime;
486 me.connection->firsttime = CurrentTime;
487
488 SetMe(&me);
489 make_server(&me);
490
491 hash_add_id(&me);
492 hash_add_client(&me);
493
494 dlinkAdd(&me, make_dlink_node(), &global_server_list);
495 dlinkAdd(&me, &me.node, &global_client_list);
496
497 load_kline_database(ConfigGeneral.klinefile);
498 load_dline_database(ConfigGeneral.dlinefile);
499 load_xline_database(ConfigGeneral.xlinefile);
500 load_resv_database(ConfigGeneral.resvfile);
501
502 load_all_modules(1);
503 load_conf_modules();
504 load_core_modules(1);
505
506 write_pidfile(pidFileName);
507
508 event_addish(&event_cleanup_tklines, NULL);
509
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 event_addish(&event_try_connections, NULL);
513
514 /* Setup the timeout check. I'll shift it later :) -- adrian */
515 event_add(&event_comm_checktimeouts, NULL);
516
517 event_addish(&event_save_all_databases, NULL);
518
519 if (ConfigServerHide.flatten_links_delay && event_write_links_file.active == 0)
520 {
521 event_write_links_file.when = ConfigServerHide.flatten_links_delay;
522 event_add(&event_write_links_file, NULL);
523 }
524
525 ilog(LOG_TYPE_IRCD, "Server Ready");
526 io_loop();
527
528 return 0;
529 }

Properties

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