ngircd.c 19 KB

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