ngircd.c 19 KB

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