ngircd.c 19 KB

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