ngircd.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2013 Alexander Barton (alex@barton.de) and Contributors.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * Please read the file COPYING, README and AUTHORS for more information.
  10. */
  11. #include "portab.h"
  12. /**
  13. * @file
  14. * The main program, including the C function main() which is called
  15. * by the loader of the operating system.
  16. */
  17. #include "imp.h"
  18. #include <assert.h>
  19. #include <errno.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <signal.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include <time.h>
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include <fcntl.h>
  29. #include <pwd.h>
  30. #include <grp.h>
  31. #if defined(DEBUG) && defined(HAVE_MTRACE)
  32. #include <mcheck.h>
  33. #endif
  34. #include "defines.h"
  35. #include "conn.h"
  36. #include "class.h"
  37. #include "conf-ssl.h"
  38. #include "channel.h"
  39. #include "conf.h"
  40. #include "lists.h"
  41. #include "log.h"
  42. #include "parse.h"
  43. #include "sighandlers.h"
  44. #include "io.h"
  45. #include "irc.h"
  46. #include "exp.h"
  47. #include "ngircd.h"
  48. static void Show_Version PARAMS(( void ));
  49. static void Show_Help PARAMS(( void ));
  50. static void Pidfile_Create PARAMS(( pid_t pid ));
  51. static void Pidfile_Delete PARAMS(( void ));
  52. static void Fill_Version PARAMS(( void ));
  53. static void Random_Init PARAMS(( void ));
  54. static void Setup_FDStreams PARAMS(( int fd ));
  55. static bool NGIRCd_Init PARAMS(( bool ));
  56. /**
  57. * The main() function of ngIRCd.
  58. *
  59. * Here all starts: this function is called by the operating system loader,
  60. * it is the first portion of code executed of ngIRCd.
  61. *
  62. * @param argc The number of arguments passed to ngIRCd on the command line.
  63. * @param argv An array containing all the arguments passed to ngIRCd.
  64. * @return Global exit code of ngIRCd, zero on success.
  65. */
  66. GLOBAL int
  67. main(int argc, const char *argv[])
  68. {
  69. bool ok, configtest = false;
  70. bool NGIRCd_NoDaemon = false;
  71. int i;
  72. size_t n;
  73. #if defined(DEBUG) && defined(HAVE_MTRACE)
  74. /* enable GNU libc memory tracing when running in debug mode
  75. * and functionality available */
  76. mtrace();
  77. #endif
  78. umask(0077);
  79. NGIRCd_SignalQuit = NGIRCd_SignalRestart = false;
  80. NGIRCd_Passive = false;
  81. #ifdef DEBUG
  82. NGIRCd_Debug = false;
  83. #endif
  84. #ifdef SNIFFER
  85. NGIRCd_Sniffer = false;
  86. #endif
  87. strlcpy(NGIRCd_ConfFile, SYSCONFDIR, sizeof(NGIRCd_ConfFile));
  88. strlcat(NGIRCd_ConfFile, CONFIG_FILE, sizeof(NGIRCd_ConfFile));
  89. Fill_Version();
  90. /* parse conmmand line */
  91. for (i = 1; i < argc; i++) {
  92. ok = false;
  93. if (argv[i][0] == '-' && argv[i][1] == '-') {
  94. /* long option */
  95. if (strcmp(argv[i], "--config") == 0) {
  96. if (i + 1 < argc) {
  97. /* Ok, there's an parameter left */
  98. strlcpy(NGIRCd_ConfFile, argv[i+1],
  99. sizeof(NGIRCd_ConfFile));
  100. /* next parameter */
  101. i++; ok = true;
  102. }
  103. }
  104. if (strcmp(argv[i], "--configtest") == 0) {
  105. configtest = true;
  106. ok = true;
  107. }
  108. #ifdef DEBUG
  109. if (strcmp(argv[i], "--debug") == 0) {
  110. NGIRCd_Debug = true;
  111. ok = true;
  112. }
  113. #endif
  114. if (strcmp(argv[i], "--help") == 0) {
  115. Show_Version();
  116. puts(""); Show_Help( ); puts( "" );
  117. exit(1);
  118. }
  119. if (strcmp(argv[i], "--nodaemon") == 0) {
  120. NGIRCd_NoDaemon = true;
  121. ok = true;
  122. }
  123. if (strcmp(argv[i], "--passive") == 0) {
  124. NGIRCd_Passive = true;
  125. ok = true;
  126. }
  127. #ifdef SNIFFER
  128. if (strcmp(argv[i], "--sniffer") == 0) {
  129. NGIRCd_Sniffer = true;
  130. ok = true;
  131. }
  132. #endif
  133. if (strcmp(argv[i], "--version") == 0) {
  134. Show_Version();
  135. exit(1);
  136. }
  137. }
  138. else if(argv[i][0] == '-' && argv[i][1] != '-') {
  139. /* short option */
  140. for (n = 1; n < strlen(argv[i]); n++) {
  141. ok = false;
  142. #ifdef DEBUG
  143. if (argv[i][n] == 'd') {
  144. NGIRCd_Debug = true;
  145. ok = true;
  146. }
  147. #endif
  148. if (argv[i][n] == 'f') {
  149. if (!argv[i][n+1] && i+1 < argc) {
  150. /* Ok, next character is a blank */
  151. strlcpy(NGIRCd_ConfFile, argv[i+1],
  152. sizeof(NGIRCd_ConfFile));
  153. /* go to the following parameter */
  154. i++;
  155. n = strlen(argv[i]);
  156. ok = true;
  157. }
  158. }
  159. if (argv[i][n] == 'h') {
  160. Show_Version();
  161. puts(""); Show_Help(); puts("");
  162. exit(1);
  163. }
  164. if (argv[i][n] == 'n') {
  165. NGIRCd_NoDaemon = true;
  166. ok = true;
  167. }
  168. if (argv[i][n] == 'p') {
  169. NGIRCd_Passive = true;
  170. ok = true;
  171. }
  172. #ifdef SNIFFER
  173. if (argv[i][n] == 's') {
  174. NGIRCd_Sniffer = true;
  175. ok = true;
  176. }
  177. #endif
  178. if (argv[i][n] == 't') {
  179. configtest = true;
  180. ok = true;
  181. }
  182. if (argv[i][n] == 'V') {
  183. Show_Version();
  184. exit(1);
  185. }
  186. if (!ok) {
  187. printf("%s: invalid option \"-%c\"!\n",
  188. PACKAGE_NAME, argv[i][n]);
  189. printf("Try \"%s --help\" for more information.\n",
  190. PACKAGE_NAME);
  191. exit(1);
  192. }
  193. }
  194. }
  195. if (!ok) {
  196. printf("%s: invalid option \"%s\"!\n",
  197. PACKAGE_NAME, argv[i]);
  198. printf("Try \"%s --help\" for more information.\n",
  199. PACKAGE_NAME);
  200. exit(1);
  201. }
  202. }
  203. /* Debug level for "VERSION" command */
  204. NGIRCd_DebugLevel[0] = '\0';
  205. #ifdef DEBUG
  206. if (NGIRCd_Debug)
  207. strcpy(NGIRCd_DebugLevel, "1");
  208. #endif
  209. #ifdef SNIFFER
  210. if (NGIRCd_Sniffer) {
  211. NGIRCd_Debug = true;
  212. strcpy(NGIRCd_DebugLevel, "2");
  213. }
  214. #endif
  215. if (configtest) {
  216. Show_Version(); puts("");
  217. exit(Conf_Test());
  218. }
  219. while (!NGIRCd_SignalQuit) {
  220. /* Initialize global variables */
  221. NGIRCd_Start = time(NULL);
  222. (void)strftime(NGIRCd_StartStr, 64,
  223. "%a %b %d %Y at %H:%M:%S (%Z)",
  224. localtime(&NGIRCd_Start));
  225. NGIRCd_SignalRestart = false;
  226. NGIRCd_SignalQuit = false;
  227. /* Initialize modules, part I */
  228. Log_Init(!NGIRCd_NoDaemon);
  229. Random_Init();
  230. Conf_Init();
  231. Log_ReInit();
  232. /* Initialize the "main program": chroot environment, user and
  233. * group ID, ... */
  234. if (!NGIRCd_Init(NGIRCd_NoDaemon)) {
  235. Log(LOG_ALERT, "Fatal: Initialization failed");
  236. exit(1);
  237. }
  238. /* Initialize modules, part II: these functions are eventually
  239. * called with already dropped privileges ... */
  240. Channel_Init();
  241. Client_Init();
  242. Conn_Init();
  243. Class_Init();
  244. if (!io_library_init(CONNECTION_POOL)) {
  245. Log(LOG_ALERT,
  246. "Fatal: Could not initialize IO routines: %s",
  247. strerror(errno));
  248. exit(1);
  249. }
  250. if (!Signals_Init()) {
  251. Log(LOG_ALERT,
  252. "Fatal: Could not set up signal handlers: %s",
  253. strerror(errno));
  254. exit(1);
  255. }
  256. /* Create protocol and server identification. The syntax
  257. * used by ngIRCd in PASS commands and the known "extended
  258. * flags" are described in doc/Protocol.txt. */
  259. #ifdef IRCPLUS
  260. snprintf(NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s:%s",
  261. PROTOVER, PROTOIRCPLUS, PACKAGE_NAME, PACKAGE_VERSION,
  262. IRCPLUSFLAGS);
  263. #ifdef ZLIB
  264. strcat(NGIRCd_ProtoID, "Z");
  265. #endif
  266. if (Conf_OperCanMode)
  267. strcat(NGIRCd_ProtoID, "o");
  268. #else /* IRCPLUS */
  269. snprintf(NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s",
  270. PROTOVER, PROTOIRC, PACKAGE_NAME, PACKAGE_VERSION);
  271. #endif /* IRCPLUS */
  272. strlcat(NGIRCd_ProtoID, " P", sizeof NGIRCd_ProtoID);
  273. #ifdef ZLIB
  274. strlcat(NGIRCd_ProtoID, "Z", sizeof NGIRCd_ProtoID);
  275. #endif
  276. LogDebug("Protocol and server ID is \"%s\".", NGIRCd_ProtoID);
  277. Channel_InitPredefined();
  278. if (Conn_InitListeners() < 1) {
  279. Log(LOG_ALERT,
  280. "Server isn't listening on a single port!" );
  281. Log(LOG_ALERT,
  282. "%s exiting due to fatal errors!", PACKAGE_NAME);
  283. Pidfile_Delete();
  284. exit(1);
  285. }
  286. /* Main Run Loop */
  287. Conn_Handler();
  288. Conn_Exit();
  289. Client_Exit();
  290. Channel_Exit();
  291. Class_Exit();
  292. Log_Exit();
  293. Signals_Exit();
  294. }
  295. Pidfile_Delete();
  296. return 0;
  297. } /* main */
  298. /**
  299. * Generate ngIRCd "version strings".
  300. *
  301. * The ngIRCd version information is generated once and then stored in the
  302. * NGIRCd_Version and NGIRCd_VersionAddition string variables for further
  303. * usage, for example by the IRC command "VERSION" and the --version command
  304. * line switch.
  305. */
  306. static void
  307. Fill_Version(void)
  308. {
  309. NGIRCd_VersionAddition[0] = '\0';
  310. #ifdef ICONV
  311. if (NGIRCd_VersionAddition[0])
  312. strlcat(NGIRCd_VersionAddition, "+",
  313. sizeof NGIRCd_VersionAddition);
  314. strlcat(NGIRCd_VersionAddition, "CHARCONV",
  315. sizeof NGIRCd_VersionAddition);
  316. #endif
  317. #ifdef DEBUG
  318. if (NGIRCd_VersionAddition[0])
  319. strlcat(NGIRCd_VersionAddition, "+",
  320. sizeof NGIRCd_VersionAddition);
  321. strlcat(NGIRCd_VersionAddition, "DEBUG",
  322. sizeof NGIRCd_VersionAddition);
  323. #endif
  324. #ifdef IDENTAUTH
  325. if (NGIRCd_VersionAddition[0])
  326. strlcat(NGIRCd_VersionAddition, "+",
  327. sizeof NGIRCd_VersionAddition);
  328. strlcat(NGIRCd_VersionAddition, "IDENT",
  329. sizeof NGIRCd_VersionAddition);
  330. #endif
  331. #ifdef WANT_IPV6
  332. if (NGIRCd_VersionAddition[0])
  333. strlcat(NGIRCd_VersionAddition, "+",
  334. sizeof(NGIRCd_VersionAddition));
  335. strlcat(NGIRCd_VersionAddition, "IPv6",
  336. sizeof(NGIRCd_VersionAddition));
  337. #endif
  338. #ifdef IRCPLUS
  339. if (NGIRCd_VersionAddition[0])
  340. strlcat(NGIRCd_VersionAddition, "+",
  341. sizeof NGIRCd_VersionAddition);
  342. strlcat(NGIRCd_VersionAddition, "IRCPLUS",
  343. sizeof NGIRCd_VersionAddition);
  344. #endif
  345. #ifdef PAM
  346. if (NGIRCd_VersionAddition[0])
  347. strlcat(NGIRCd_VersionAddition, "+",
  348. sizeof NGIRCd_VersionAddition);
  349. strlcat(NGIRCd_VersionAddition, "PAM",
  350. sizeof NGIRCd_VersionAddition);
  351. #endif
  352. #ifdef STRICT_RFC
  353. if (NGIRCd_VersionAddition[0])
  354. strlcat(NGIRCd_VersionAddition, "+",
  355. sizeof NGIRCd_VersionAddition);
  356. strlcat(NGIRCd_VersionAddition, "RFC",
  357. sizeof NGIRCd_VersionAddition);
  358. #endif
  359. #ifdef SNIFFER
  360. if (NGIRCd_VersionAddition[0])
  361. strlcat(NGIRCd_VersionAddition, "+",
  362. sizeof NGIRCd_VersionAddition);
  363. strlcat(NGIRCd_VersionAddition, "SNIFFER",
  364. sizeof NGIRCd_VersionAddition);
  365. #endif
  366. #ifdef SSL_SUPPORT
  367. if (NGIRCd_VersionAddition[0])
  368. strlcat(NGIRCd_VersionAddition, "+",
  369. sizeof NGIRCd_VersionAddition);
  370. strlcat(NGIRCd_VersionAddition, "SSL",
  371. sizeof NGIRCd_VersionAddition);
  372. #endif
  373. #ifdef SYSLOG
  374. if (NGIRCd_VersionAddition[0])
  375. strlcat(NGIRCd_VersionAddition, "+",
  376. sizeof NGIRCd_VersionAddition);
  377. strlcat(NGIRCd_VersionAddition, "SYSLOG",
  378. sizeof NGIRCd_VersionAddition);
  379. #endif
  380. #ifdef TCPWRAP
  381. if (NGIRCd_VersionAddition[0])
  382. strlcat(NGIRCd_VersionAddition, "+",
  383. sizeof NGIRCd_VersionAddition);
  384. strlcat(NGIRCd_VersionAddition, "TCPWRAP",
  385. sizeof NGIRCd_VersionAddition);
  386. #endif
  387. #ifdef ZLIB
  388. if (NGIRCd_VersionAddition[0])
  389. strlcat(NGIRCd_VersionAddition, "+",
  390. sizeof NGIRCd_VersionAddition);
  391. strlcat(NGIRCd_VersionAddition, "ZLIB",
  392. sizeof NGIRCd_VersionAddition);
  393. #endif
  394. if (NGIRCd_VersionAddition[0])
  395. strlcat(NGIRCd_VersionAddition, "-",
  396. sizeof(NGIRCd_VersionAddition));
  397. strlcat(NGIRCd_VersionAddition, HOST_CPU,
  398. sizeof(NGIRCd_VersionAddition));
  399. strlcat(NGIRCd_VersionAddition, "/", sizeof(NGIRCd_VersionAddition));
  400. strlcat(NGIRCd_VersionAddition, HOST_VENDOR,
  401. sizeof(NGIRCd_VersionAddition));
  402. strlcat(NGIRCd_VersionAddition, "/", sizeof(NGIRCd_VersionAddition));
  403. strlcat(NGIRCd_VersionAddition, HOST_OS,
  404. sizeof(NGIRCd_VersionAddition));
  405. snprintf(NGIRCd_Version, sizeof NGIRCd_Version, "%s %s-%s",
  406. PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition);
  407. } /* Fill_Version */
  408. /**
  409. * Display copyright and version information of ngIRCd on the console.
  410. */
  411. static void
  412. Show_Version( void )
  413. {
  414. puts( NGIRCd_Version );
  415. puts( "Copyright (c)2001-2013 Alexander Barton (<alex@barton.de>) and Contributors." );
  416. puts( "Homepage: <http://ngircd.barton.de/>\n" );
  417. puts( "This is free software; see the source for copying conditions. There is NO" );
  418. puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
  419. } /* Show_Version */
  420. /**
  421. * Display a short help text on the console.
  422. * This help depends on the configuration of the executable and only shows
  423. * options that are actually enabled.
  424. */
  425. static void
  426. Show_Help( void )
  427. {
  428. #ifdef DEBUG
  429. puts( " -d, --debug log extra debug messages" );
  430. #endif
  431. puts( " -f, --config <f> use file <f> as configuration file" );
  432. puts( " -n, --nodaemon don't fork and don't detach from controlling terminal" );
  433. puts( " -p, --passive disable automatic connections to other servers" );
  434. #ifdef SNIFFER
  435. puts( " -s, --sniffer enable network sniffer and display all IRC traffic" );
  436. #endif
  437. puts( " -t, --configtest read, validate and display configuration; then exit" );
  438. puts( " -V, --version output version information and exit" );
  439. puts( " -h, --help display this help and exit" );
  440. } /* Show_Help */
  441. /**
  442. * Delete the file containing the process ID (PID).
  443. */
  444. static void
  445. Pidfile_Delete( void )
  446. {
  447. /* Pidfile configured? */
  448. if( ! Conf_PidFile[0] ) return;
  449. #ifdef DEBUG
  450. Log( LOG_DEBUG, "Removing PID file (%s) ...", Conf_PidFile );
  451. #endif
  452. if( unlink( Conf_PidFile ))
  453. Log( LOG_ERR, "Error unlinking PID file (%s): %s", Conf_PidFile, strerror( errno ));
  454. } /* Pidfile_Delete */
  455. /**
  456. * Create the file containing the process ID of ngIRCd ("PID file").
  457. *
  458. * @param pid The process ID to be stored in this file.
  459. */
  460. static void
  461. Pidfile_Create(pid_t pid)
  462. {
  463. int pidfd;
  464. char pidbuf[64];
  465. int len;
  466. /* Pidfile configured? */
  467. if( ! Conf_PidFile[0] ) return;
  468. #ifdef DEBUG
  469. Log( LOG_DEBUG, "Creating PID file (%s) ...", Conf_PidFile );
  470. #endif
  471. pidfd = open( Conf_PidFile, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
  472. if ( pidfd < 0 ) {
  473. Log( LOG_ERR, "Error writing PID file (%s): %s", Conf_PidFile, strerror( errno ));
  474. return;
  475. }
  476. len = snprintf(pidbuf, sizeof pidbuf, "%ld\n", (long)pid);
  477. if (len < 0 || len >= (int)sizeof pidbuf) {
  478. Log(LOG_ERR, "Error converting pid");
  479. close(pidfd);
  480. return;
  481. }
  482. if (write(pidfd, pidbuf, (size_t)len) != (ssize_t)len)
  483. Log( LOG_ERR, "Can't write PID file (%s): %s", Conf_PidFile, strerror( errno ));
  484. if( close(pidfd) != 0 )
  485. Log( LOG_ERR, "Error closing PID file (%s): %s", Conf_PidFile, strerror( errno ));
  486. } /* Pidfile_Create */
  487. /**
  488. * Redirect stdin, stdout and stderr to apropriate file handles.
  489. *
  490. * @param fd The file handle stdin, stdout and stderr should be redirected to.
  491. */
  492. static void
  493. Setup_FDStreams(int fd)
  494. {
  495. if (fd < 0)
  496. return;
  497. fflush(stdout);
  498. fflush(stderr);
  499. /* Create new stdin(0), stdout(1) and stderr(2) descriptors */
  500. dup2( fd, 0 ); dup2( fd, 1 ); dup2( fd, 2 );
  501. } /* Setup_FDStreams */
  502. #if !defined(SINGLE_USER_OS)
  503. /**
  504. * Get user and group ID of unprivileged "nobody" user.
  505. *
  506. * @param uid User ID
  507. * @param gid Group ID
  508. * @return true on success.
  509. */
  510. static bool
  511. NGIRCd_getNobodyID(uid_t *uid, gid_t *gid )
  512. {
  513. struct passwd *pwd;
  514. #ifdef __CYGWIN__
  515. /* Cygwin kludge.
  516. * It can return EINVAL instead of EPERM
  517. * so, if we are already unprivileged,
  518. * use id of current user.
  519. */
  520. if (geteuid() && getuid()) {
  521. *uid = getuid();
  522. *gid = getgid();
  523. return true;
  524. }
  525. #endif
  526. pwd = getpwnam("nobody");
  527. if (!pwd)
  528. return false;
  529. if (!pwd->pw_uid || !pwd->pw_gid)
  530. return false;
  531. *uid = pwd->pw_uid;
  532. *gid = pwd->pw_gid;
  533. endpwent();
  534. return true;
  535. } /* NGIRCd_getNobodyID */
  536. #endif
  537. static bool
  538. Random_Init_Kern(const char *file)
  539. {
  540. unsigned int seed;
  541. bool ret = false;
  542. int fd = open(file, O_RDONLY);
  543. if (fd >= 0) {
  544. if (read(fd, &seed, sizeof(seed)) == sizeof(seed))
  545. ret = true;
  546. close(fd);
  547. srand(seed);
  548. }
  549. return ret;
  550. }
  551. /**
  552. * Initialize libc rand(3) number generator
  553. */
  554. static void
  555. Random_Init(void)
  556. {
  557. if (Random_Init_Kern("/dev/urandom"))
  558. return;
  559. if (Random_Init_Kern("/dev/random"))
  560. return;
  561. if (Random_Init_Kern("/dev/arandom"))
  562. return;
  563. srand(rand() ^ (unsigned)getpid() ^ (unsigned)time(NULL));
  564. }
  565. /**
  566. * Initialize ngIRCd daemon.
  567. *
  568. * @param NGIRCd_NoDaemon Set to true if ngIRCd should run in the
  569. * foreground (and not as a daemon).
  570. * @return true on success.
  571. */
  572. static bool
  573. NGIRCd_Init(bool NGIRCd_NoDaemon)
  574. {
  575. static bool initialized;
  576. bool chrooted = false;
  577. struct passwd *pwd;
  578. struct group *grp;
  579. int real_errno, fd = -1;
  580. pid_t pid;
  581. if (initialized)
  582. return true;
  583. if (!NGIRCd_NoDaemon) {
  584. /* open /dev/null before chroot() */
  585. fd = open( "/dev/null", O_RDWR);
  586. if (fd < 0)
  587. Log(LOG_WARNING, "Could not open /dev/null: %s",
  588. strerror(errno));
  589. }
  590. /* SSL initialization */
  591. if (!ConnSSL_InitLibrary())
  592. Log(LOG_WARNING,
  593. "Error during SSL initialization, continuing without SSL ...");
  594. /* Change root */
  595. if (Conf_Chroot[0]) {
  596. if (chdir(Conf_Chroot) != 0) {
  597. Log(LOG_ERR, "Can't chdir() in ChrootDir (%s): %s",
  598. Conf_Chroot, strerror(errno));
  599. goto out;
  600. }
  601. if (chroot(Conf_Chroot) != 0) {
  602. Log(LOG_ERR,
  603. "Can't change root directory to \"%s\": %s",
  604. Conf_Chroot, strerror(errno));
  605. goto out;
  606. } else {
  607. chrooted = true;
  608. Log(LOG_INFO,
  609. "Changed root and working directory to \"%s\".",
  610. Conf_Chroot);
  611. }
  612. }
  613. #if !defined(SINGLE_USER_OS)
  614. /* Check user ID */
  615. if (Conf_UID == 0) {
  616. pwd = getpwuid(0);
  617. Log(LOG_INFO,
  618. "ServerUID must not be %s(0), using \"nobody\" instead.",
  619. pwd ? pwd->pw_name : "?");
  620. if (!NGIRCd_getNobodyID(&Conf_UID, &Conf_GID)) {
  621. Log(LOG_WARNING,
  622. "Could not get user/group ID of user \"nobody\": %s",
  623. errno ? strerror(errno) : "not found" );
  624. goto out;
  625. }
  626. }
  627. /* Change group ID */
  628. if (getgid() != Conf_GID) {
  629. if (setgid(Conf_GID) != 0) {
  630. real_errno = errno;
  631. grp = getgrgid(Conf_GID);
  632. Log(LOG_ERR, "Can't change group ID to %s(%u): %s",
  633. grp ? grp->gr_name : "?", Conf_GID,
  634. strerror(errno));
  635. if (real_errno != EPERM)
  636. goto out;
  637. }
  638. }
  639. #endif
  640. /* Change user ID */
  641. if (getuid() != Conf_UID) {
  642. if (setuid(Conf_UID) != 0) {
  643. real_errno = errno;
  644. pwd = getpwuid(Conf_UID);
  645. Log(LOG_ERR, "Can't change user ID to %s(%u): %s",
  646. pwd ? pwd->pw_name : "?", Conf_UID,
  647. strerror(errno));
  648. if (real_errno != EPERM)
  649. goto out;
  650. }
  651. }
  652. initialized = true;
  653. /* Normally a child process is forked which isn't any longer
  654. * connected to ther controlling terminal. Use "--nodaemon"
  655. * to disable this "daemon mode" (useful for debugging). */
  656. if (!NGIRCd_NoDaemon) {
  657. pid = fork();
  658. if (pid > 0) {
  659. /* "Old" process: exit. */
  660. exit(0);
  661. }
  662. if (pid < 0) {
  663. /* Error!? */
  664. fprintf(stderr,
  665. "%s: Can't fork: %s!\nFatal error, exiting now ...\n",
  666. PACKAGE_NAME, strerror(errno));
  667. exit(1);
  668. }
  669. /* New child process */
  670. #ifdef HAVE_SETSID
  671. (void)setsid();
  672. #else
  673. setpgrp(0, getpid());
  674. #endif
  675. if (chdir("/") != 0)
  676. Log(LOG_ERR, "Can't change directory to '/': %s",
  677. strerror(errno));
  678. /* Detach stdin, stdout and stderr */
  679. Setup_FDStreams(fd);
  680. if (fd > 2)
  681. close(fd);
  682. }
  683. pid = getpid();
  684. Pidfile_Create(pid);
  685. /* Check UID/GID we are running as, can be different from values
  686. * configured (e. g. if we were already started with a UID>0. */
  687. Conf_UID = getuid();
  688. Conf_GID = getgid();
  689. pwd = getpwuid(Conf_UID);
  690. grp = getgrgid(Conf_GID);
  691. Log(LOG_INFO, "Running as user %s(%ld), group %s(%ld), with PID %ld.",
  692. pwd ? pwd->pw_name : "unknown", (long)Conf_UID,
  693. grp ? grp->gr_name : "unknown", (long)Conf_GID, (long)pid);
  694. if (chrooted) {
  695. Log(LOG_INFO, "Running with root directory \"%s\".",
  696. Conf_Chroot );
  697. return true;
  698. } else
  699. Log(LOG_INFO, "Not running with changed root directory.");
  700. /* Change working directory to home directory of the user we are
  701. * running as (only when running in daemon mode and not in chroot) */
  702. if (NGIRCd_NoDaemon)
  703. return true;
  704. if (pwd) {
  705. if (chdir(pwd->pw_dir) == 0)
  706. Log(LOG_DEBUG,
  707. "Changed working directory to \"%s\" ...",
  708. pwd->pw_dir);
  709. else
  710. Log(LOG_INFO,
  711. "Notice: Can't change working directory to \"%s\": %s",
  712. pwd->pw_dir, strerror(errno));
  713. } else
  714. Log(LOG_ERR, "Can't get user informaton for UID %d!?", Conf_UID);
  715. return true;
  716. out:
  717. if (fd > 2)
  718. close(fd);
  719. return false;
  720. } /* NGIRCd_Init */
  721. /* -eof- */