ngircd.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2021 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. #define GLOBAL_INIT
  12. #include "portab.h"
  13. /**
  14. * @file
  15. * The main program, including the C function main() which is called
  16. * by the loader of the operating system.
  17. */
  18. #include <assert.h>
  19. #include <errno.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include <time.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <fcntl.h>
  28. #include <pwd.h>
  29. #include <grp.h>
  30. #if defined(DEBUG) && defined(HAVE_MTRACE)
  31. #include <mcheck.h>
  32. #endif
  33. #include "conn.h"
  34. #include "class.h"
  35. #include "channel.h"
  36. #include "conf.h"
  37. #include "log.h"
  38. #include "sighandlers.h"
  39. #include "io.h"
  40. #include "ngircd.h"
  41. static void Show_Version PARAMS(( void ));
  42. static void Show_Help PARAMS(( void ));
  43. static void Pidfile_Create PARAMS(( pid_t pid ));
  44. static void Pidfile_Delete PARAMS(( void ));
  45. static void Fill_Version PARAMS(( void ));
  46. static void Random_Init PARAMS(( void ));
  47. static void Setup_FDStreams PARAMS(( int fd ));
  48. static bool NGIRCd_Init PARAMS(( bool ));
  49. /**
  50. * The main() function of ngIRCd.
  51. *
  52. * Here all starts: this function is called by the operating system loader,
  53. * it is the first portion of code executed of ngIRCd.
  54. *
  55. * @param argc The number of arguments passed to ngIRCd on the command line.
  56. * @param argv An array containing all the arguments passed to ngIRCd.
  57. * @return Global exit code of ngIRCd, zero on success.
  58. */
  59. GLOBAL int
  60. main(int argc, const char *argv[])
  61. {
  62. bool ok, configtest = false;
  63. bool NGIRCd_NoDaemon = false;
  64. int i;
  65. size_t n;
  66. #if defined(DEBUG) && defined(HAVE_MTRACE)
  67. /* enable GNU libc memory tracing when running in debug mode
  68. * and functionality available */
  69. mtrace();
  70. #endif
  71. umask(0077);
  72. NGIRCd_SignalQuit = NGIRCd_SignalRestart = false;
  73. NGIRCd_Passive = false;
  74. #ifdef DEBUG
  75. NGIRCd_Debug = false;
  76. #endif
  77. #ifdef SNIFFER
  78. NGIRCd_Sniffer = false;
  79. #endif
  80. strlcpy(NGIRCd_ConfFile, SYSCONFDIR, sizeof(NGIRCd_ConfFile));
  81. strlcat(NGIRCd_ConfFile, CONFIG_FILE, sizeof(NGIRCd_ConfFile));
  82. Fill_Version();
  83. /* parse conmmand line */
  84. for (i = 1; i < argc; i++) {
  85. ok = false;
  86. if (argv[i][0] == '-' && argv[i][1] == '-') {
  87. /* long option */
  88. if (strcmp(argv[i], "--config") == 0) {
  89. if (i + 1 < argc) {
  90. /* Ok, there's an parameter left */
  91. strlcpy(NGIRCd_ConfFile, argv[i+1],
  92. sizeof(NGIRCd_ConfFile));
  93. /* next parameter */
  94. i++; ok = true;
  95. }
  96. }
  97. if (strcmp(argv[i], "--configtest") == 0) {
  98. configtest = true;
  99. ok = true;
  100. }
  101. #ifdef DEBUG
  102. if (strcmp(argv[i], "--debug") == 0) {
  103. NGIRCd_Debug = true;
  104. ok = true;
  105. }
  106. #endif
  107. if (strcmp(argv[i], "--help") == 0) {
  108. Show_Version();
  109. puts(""); Show_Help( ); puts( "" );
  110. exit(0);
  111. }
  112. if (strcmp(argv[i], "--nodaemon") == 0) {
  113. NGIRCd_NoDaemon = true;
  114. ok = true;
  115. }
  116. if (strcmp(argv[i], "--passive") == 0) {
  117. NGIRCd_Passive = true;
  118. ok = true;
  119. }
  120. #ifdef SNIFFER
  121. if (strcmp(argv[i], "--sniffer") == 0) {
  122. NGIRCd_Sniffer = true;
  123. ok = true;
  124. }
  125. #endif
  126. if (strcmp(argv[i], "--version") == 0) {
  127. Show_Version();
  128. exit(0);
  129. }
  130. }
  131. else if(argv[i][0] == '-' && argv[i][1] != '-') {
  132. /* short option */
  133. for (n = 1; n < strlen(argv[i]); n++) {
  134. ok = false;
  135. #ifdef DEBUG
  136. if (argv[i][n] == 'd') {
  137. NGIRCd_Debug = true;
  138. ok = true;
  139. }
  140. #endif
  141. if (argv[i][n] == 'f') {
  142. if (!argv[i][n+1] && i+1 < argc) {
  143. /* Ok, next character is a blank */
  144. strlcpy(NGIRCd_ConfFile, argv[i+1],
  145. sizeof(NGIRCd_ConfFile));
  146. /* go to the following parameter */
  147. i++;
  148. n = strlen(argv[i]);
  149. ok = true;
  150. }
  151. }
  152. if (argv[i][n] == 'h') {
  153. Show_Version();
  154. puts(""); Show_Help(); puts("");
  155. exit(1);
  156. }
  157. if (argv[i][n] == 'n') {
  158. NGIRCd_NoDaemon = true;
  159. ok = true;
  160. }
  161. if (argv[i][n] == 'p') {
  162. NGIRCd_Passive = true;
  163. ok = true;
  164. }
  165. #ifdef SNIFFER
  166. if (argv[i][n] == 's') {
  167. NGIRCd_Sniffer = true;
  168. ok = true;
  169. }
  170. #endif
  171. if (argv[i][n] == 't') {
  172. configtest = true;
  173. ok = true;
  174. }
  175. if (argv[i][n] == 'V') {
  176. Show_Version();
  177. exit(1);
  178. }
  179. if (!ok) {
  180. fprintf(stderr,
  181. "%s: invalid option \"-%c\"!\n",
  182. PACKAGE_NAME, argv[i][n]);
  183. fprintf(stderr,
  184. "Try \"%s --help\" for more information.\n",
  185. PACKAGE_NAME);
  186. exit(2);
  187. }
  188. }
  189. }
  190. if (!ok) {
  191. fprintf(stderr, "%s: invalid option \"%s\"!\n",
  192. PACKAGE_NAME, argv[i]);
  193. fprintf(stderr, "Try \"%s --help\" for more information.\n",
  194. PACKAGE_NAME);
  195. exit(2);
  196. }
  197. }
  198. /* Debug level for "VERSION" command */
  199. NGIRCd_DebugLevel[0] = '\0';
  200. #ifdef DEBUG
  201. if (NGIRCd_Debug)
  202. strcpy(NGIRCd_DebugLevel, "1");
  203. #endif
  204. #ifdef SNIFFER
  205. if (NGIRCd_Sniffer) {
  206. NGIRCd_Debug = true;
  207. strcpy(NGIRCd_DebugLevel, "2");
  208. }
  209. #endif
  210. if (configtest) {
  211. Show_Version(); puts("");
  212. exit(Conf_Test());
  213. }
  214. while (!NGIRCd_SignalQuit) {
  215. /* Initialize global variables */
  216. NGIRCd_Start = time(NULL);
  217. (void)strftime(NGIRCd_StartStr, 64,
  218. "%a %b %d %Y at %H:%M:%S (%Z)",
  219. localtime(&NGIRCd_Start));
  220. NGIRCd_SignalRestart = false;
  221. NGIRCd_SignalQuit = false;
  222. Log_Init(!NGIRCd_NoDaemon);
  223. Random_Init();
  224. Conf_Init();
  225. Log_ReInit();
  226. /* Initialize the "main program":
  227. * chroot environment, user and group ID, ... */
  228. if (!NGIRCd_Init(NGIRCd_NoDaemon)) {
  229. Log(LOG_ALERT, "Fatal: Initialization failed, exiting!");
  230. exit(1);
  231. }
  232. if (!io_library_init(CONNECTION_POOL)) {
  233. Log(LOG_ALERT,
  234. "Fatal: Could not initialize IO routines: %s",
  235. strerror(errno));
  236. exit(1);
  237. }
  238. if (!Signals_Init()) {
  239. Log(LOG_ALERT,
  240. "Fatal: Could not set up signal handlers: %s",
  241. strerror(errno));
  242. exit(1);
  243. }
  244. Channel_Init();
  245. Conn_Init();
  246. Class_Init();
  247. Client_Init();
  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-2021 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. #ifdef HAVE_SETGROUPS
  641. if (setgroups(0, NULL) != 0) {
  642. real_errno = errno;
  643. Log(LOG_ERR, "Can't drop supplementary group IDs: %s!",
  644. strerror(errno));
  645. if (real_errno != EPERM)
  646. goto out;
  647. }
  648. #else
  649. Log(LOG_WARNING,
  650. "Can't drop supplementary group IDs: setgroups(3) missing!");
  651. #endif
  652. }
  653. #endif
  654. /* Change user ID */
  655. if (getuid() != Conf_UID) {
  656. if (setuid(Conf_UID) != 0) {
  657. real_errno = errno;
  658. pwd = getpwuid(Conf_UID);
  659. Log(LOG_ERR, "Can't change user ID to %s(%u): %s!",
  660. pwd ? pwd->pw_name : "?", Conf_UID,
  661. strerror(real_errno));
  662. if (real_errno != EPERM)
  663. goto out;
  664. }
  665. }
  666. initialized = true;
  667. /* Normally a child process is forked which isn't any longer
  668. * connected to ther controlling terminal. Use "--nodaemon"
  669. * to disable this "daemon mode" (useful for debugging). */
  670. if (!NGIRCd_NoDaemon) {
  671. pid = fork();
  672. if (pid > 0) {
  673. /* "Old" process: exit. */
  674. exit(0);
  675. }
  676. if (pid < 0) {
  677. /* Error!? */
  678. fprintf(stderr,
  679. "%s: Can't fork: %s!\nFatal error, exiting now ...\n",
  680. PACKAGE_NAME, strerror(errno));
  681. exit(1);
  682. }
  683. /* New child process */
  684. #ifdef HAVE_SETSID
  685. (void)setsid();
  686. #else
  687. setpgrp(0, getpid());
  688. #endif
  689. if (chdir("/") != 0)
  690. Log(LOG_ERR, "Can't change directory to '/': %s!",
  691. strerror(errno));
  692. /* Detach stdin, stdout and stderr */
  693. Setup_FDStreams(fd);
  694. if (fd > 2)
  695. close(fd);
  696. }
  697. pid = getpid();
  698. Pidfile_Create(pid);
  699. /* Check UID/GID we are running as, can be different from values
  700. * configured (e. g. if we were already started with a UID>0. */
  701. Conf_UID = getuid();
  702. Conf_GID = getgid();
  703. pwd = getpwuid(Conf_UID);
  704. grp = getgrgid(Conf_GID);
  705. Log(LOG_INFO, "Running as user %s(%ld), group %s(%ld), with PID %ld.",
  706. pwd ? pwd->pw_name : "unknown", (long)Conf_UID,
  707. grp ? grp->gr_name : "unknown", (long)Conf_GID, (long)pid);
  708. if (chrooted) {
  709. Log(LOG_INFO, "Running with root directory \"%s\".",
  710. Conf_Chroot );
  711. return true;
  712. } else
  713. Log(LOG_INFO, "Not running with changed root directory.");
  714. /* Change working directory to home directory of the user we are
  715. * running as (only when running in daemon mode and not in chroot) */
  716. if (NGIRCd_NoDaemon)
  717. return true;
  718. if (pwd) {
  719. if (chdir(pwd->pw_dir) == 0)
  720. Log(LOG_DEBUG,
  721. "Changed working directory to \"%s\" ...",
  722. pwd->pw_dir);
  723. else
  724. Log(LOG_ERR,
  725. "Can't change working directory to \"%s\": %s!",
  726. pwd->pw_dir, strerror(errno));
  727. } else
  728. Log(LOG_ERR, "Can't get user informaton for UID %d!?", Conf_UID);
  729. return true;
  730. out:
  731. if (fd > 2)
  732. close(fd);
  733. return false;
  734. } /* NGIRCd_Init */
  735. /* -eof- */