ngircd.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2014 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, exiting!");
  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. strlcat(NGIRCd_ProtoID, "Z", sizeof NGIRCd_ProtoID);
  265. #endif
  266. if (Conf_OperCanMode)
  267. strlcat(NGIRCd_ProtoID, "o", sizeof NGIRCd_ProtoID);
  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-2014 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 process ID!");
  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,
  484. strerror(errno));
  485. if (close(pidfd) != 0)
  486. Log(LOG_ERR, "Error closing PID file (%s): %s!", Conf_PidFile,
  487. strerror(errno));
  488. } /* Pidfile_Create */
  489. /**
  490. * Redirect stdin, stdout and stderr to appropriate file handles.
  491. *
  492. * @param fd The file handle stdin, stdout and stderr should be redirected to.
  493. */
  494. static void
  495. Setup_FDStreams(int fd)
  496. {
  497. if (fd < 0)
  498. return;
  499. fflush(stdout);
  500. fflush(stderr);
  501. /* Create new stdin(0), stdout(1) and stderr(2) descriptors */
  502. dup2( fd, 0 ); dup2( fd, 1 ); dup2( fd, 2 );
  503. } /* Setup_FDStreams */
  504. #if !defined(SINGLE_USER_OS)
  505. /**
  506. * Get user and group ID of unprivileged "nobody" user.
  507. *
  508. * @param uid User ID
  509. * @param gid Group ID
  510. * @return true on success.
  511. */
  512. static bool
  513. NGIRCd_getNobodyID(uid_t *uid, gid_t *gid )
  514. {
  515. struct passwd *pwd;
  516. #ifdef __CYGWIN__
  517. /* Cygwin kludge.
  518. * It can return EINVAL instead of EPERM
  519. * so, if we are already unprivileged,
  520. * use id of current user.
  521. */
  522. if (geteuid() && getuid()) {
  523. *uid = getuid();
  524. *gid = getgid();
  525. return true;
  526. }
  527. #endif
  528. pwd = getpwnam("nobody");
  529. if (!pwd)
  530. return false;
  531. if (!pwd->pw_uid || !pwd->pw_gid)
  532. return false;
  533. *uid = pwd->pw_uid;
  534. *gid = pwd->pw_gid;
  535. endpwent();
  536. return true;
  537. } /* NGIRCd_getNobodyID */
  538. #endif
  539. #ifdef HAVE_ARC4RANDOM
  540. static void
  541. Random_Init(void)
  542. {
  543. }
  544. #else
  545. static bool
  546. Random_Init_Kern(const char *file)
  547. {
  548. unsigned int seed;
  549. bool ret = false;
  550. int fd = open(file, O_RDONLY);
  551. if (fd >= 0) {
  552. if (read(fd, &seed, sizeof(seed)) == sizeof(seed))
  553. ret = true;
  554. close(fd);
  555. srand(seed);
  556. }
  557. return ret;
  558. }
  559. /**
  560. * Initialize libc rand(3) number generator
  561. */
  562. static void
  563. Random_Init(void)
  564. {
  565. if (Random_Init_Kern("/dev/urandom"))
  566. return;
  567. if (Random_Init_Kern("/dev/random"))
  568. return;
  569. if (Random_Init_Kern("/dev/arandom"))
  570. return;
  571. srand(rand() ^ (unsigned)getpid() ^ (unsigned)time(NULL));
  572. }
  573. #endif
  574. /**
  575. * Initialize ngIRCd daemon.
  576. *
  577. * @param NGIRCd_NoDaemon Set to true if ngIRCd should run in the
  578. * foreground (and not as a daemon).
  579. * @return true on success.
  580. */
  581. static bool
  582. NGIRCd_Init(bool NGIRCd_NoDaemon)
  583. {
  584. static bool initialized;
  585. bool chrooted = false;
  586. struct passwd *pwd;
  587. struct group *grp;
  588. int real_errno, fd = -1;
  589. pid_t pid;
  590. if (initialized)
  591. return true;
  592. if (!NGIRCd_NoDaemon) {
  593. /* open /dev/null before chroot() */
  594. fd = open( "/dev/null", O_RDWR);
  595. if (fd < 0)
  596. Log(LOG_WARNING, "Could not open /dev/null: %s",
  597. strerror(errno));
  598. }
  599. /* SSL initialization */
  600. if (!ConnSSL_InitLibrary()) {
  601. Log(LOG_ERR, "Error during SSL initialization!");
  602. goto out;
  603. }
  604. /* Change root */
  605. if (Conf_Chroot[0]) {
  606. if (chdir(Conf_Chroot) != 0) {
  607. Log(LOG_ERR, "Can't chdir() in ChrootDir (%s): %s!",
  608. Conf_Chroot, strerror(errno));
  609. goto out;
  610. }
  611. if (chroot(Conf_Chroot) != 0) {
  612. Log(LOG_ERR,
  613. "Can't change root directory to \"%s\": %s!",
  614. Conf_Chroot, strerror(errno));
  615. goto out;
  616. } else {
  617. chrooted = true;
  618. Log(LOG_INFO,
  619. "Changed root and working directory to \"%s\".",
  620. Conf_Chroot);
  621. }
  622. }
  623. #if !defined(SINGLE_USER_OS)
  624. /* Check user ID */
  625. if (Conf_UID == 0) {
  626. pwd = getpwuid(0);
  627. Log(LOG_INFO,
  628. "ServerUID must not be %s(0), using \"nobody\" instead.",
  629. pwd ? pwd->pw_name : "?");
  630. if (!NGIRCd_getNobodyID(&Conf_UID, &Conf_GID)) {
  631. Log(LOG_WARNING,
  632. "Could not get user/group ID of user \"nobody\": %s",
  633. errno ? strerror(errno) : "not found" );
  634. goto out;
  635. }
  636. }
  637. /* Change group ID */
  638. if (getgid() != Conf_GID) {
  639. if (setgid(Conf_GID) != 0) {
  640. real_errno = errno;
  641. grp = getgrgid(Conf_GID);
  642. Log(LOG_ERR, "Can't change group ID to %s(%u): %s!",
  643. grp ? grp->gr_name : "?", Conf_GID,
  644. strerror(real_errno));
  645. if (real_errno != EPERM)
  646. goto out;
  647. }
  648. if (setgroups(0, NULL) != 0) {
  649. real_errno = errno;
  650. Log(LOG_ERR, "Can't drop supplementary group IDs: %s!",
  651. strerror(errno));
  652. if (real_errno != EPERM)
  653. goto out;
  654. }
  655. }
  656. #endif
  657. /* Change user ID */
  658. if (getuid() != Conf_UID) {
  659. if (setuid(Conf_UID) != 0) {
  660. real_errno = errno;
  661. pwd = getpwuid(Conf_UID);
  662. Log(LOG_ERR, "Can't change user ID to %s(%u): %s!",
  663. pwd ? pwd->pw_name : "?", Conf_UID,
  664. strerror(real_errno));
  665. if (real_errno != EPERM)
  666. goto out;
  667. }
  668. }
  669. initialized = true;
  670. /* Normally a child process is forked which isn't any longer
  671. * connected to ther controlling terminal. Use "--nodaemon"
  672. * to disable this "daemon mode" (useful for debugging). */
  673. if (!NGIRCd_NoDaemon) {
  674. pid = fork();
  675. if (pid > 0) {
  676. /* "Old" process: exit. */
  677. exit(0);
  678. }
  679. if (pid < 0) {
  680. /* Error!? */
  681. fprintf(stderr,
  682. "%s: Can't fork: %s!\nFatal error, exiting now ...\n",
  683. PACKAGE_NAME, strerror(errno));
  684. exit(1);
  685. }
  686. /* New child process */
  687. #ifdef HAVE_SETSID
  688. (void)setsid();
  689. #else
  690. setpgrp(0, getpid());
  691. #endif
  692. if (chdir("/") != 0)
  693. Log(LOG_ERR, "Can't change directory to '/': %s!",
  694. strerror(errno));
  695. /* Detach stdin, stdout and stderr */
  696. Setup_FDStreams(fd);
  697. if (fd > 2)
  698. close(fd);
  699. }
  700. pid = getpid();
  701. Pidfile_Create(pid);
  702. /* Check UID/GID we are running as, can be different from values
  703. * configured (e. g. if we were already started with a UID>0. */
  704. Conf_UID = getuid();
  705. Conf_GID = getgid();
  706. pwd = getpwuid(Conf_UID);
  707. grp = getgrgid(Conf_GID);
  708. Log(LOG_INFO, "Running as user %s(%ld), group %s(%ld), with PID %ld.",
  709. pwd ? pwd->pw_name : "unknown", (long)Conf_UID,
  710. grp ? grp->gr_name : "unknown", (long)Conf_GID, (long)pid);
  711. if (chrooted) {
  712. Log(LOG_INFO, "Running with root directory \"%s\".",
  713. Conf_Chroot );
  714. return true;
  715. } else
  716. Log(LOG_INFO, "Not running with changed root directory.");
  717. /* Change working directory to home directory of the user we are
  718. * running as (only when running in daemon mode and not in chroot) */
  719. if (NGIRCd_NoDaemon)
  720. return true;
  721. if (pwd) {
  722. if (chdir(pwd->pw_dir) == 0)
  723. Log(LOG_DEBUG,
  724. "Changed working directory to \"%s\" ...",
  725. pwd->pw_dir);
  726. else
  727. Log(LOG_ERR,
  728. "Can't change working directory to \"%s\": %s!",
  729. pwd->pw_dir, strerror(errno));
  730. } else
  731. Log(LOG_ERR, "Can't get user informaton for UID %d!?", Conf_UID);
  732. return true;
  733. out:
  734. if (fd > 2)
  735. close(fd);
  736. return false;
  737. } /* NGIRCd_Init */
  738. /* -eof- */