ngircd.c 20 KB

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