ngircd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2010 Alexander Barton (alex@barton.de).
  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. #ifdef ZEROCONF
  46. #include "rendezvous.h"
  47. #endif
  48. #include "exp.h"
  49. #include "ngircd.h"
  50. static void Show_Version PARAMS(( void ));
  51. static void Show_Help PARAMS(( void ));
  52. static void Pidfile_Create PARAMS(( pid_t pid ));
  53. static void Pidfile_Delete PARAMS(( void ));
  54. static void Fill_Version PARAMS(( void ));
  55. static void Setup_FDStreams PARAMS(( int fd ));
  56. static bool NGIRCd_Init PARAMS(( bool ));
  57. /**
  58. * The main() function of ngIRCd.
  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. * @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 IRCs "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. /* Initialize modules, part I */
  236. Log_Init( ! NGIRCd_NoDaemon );
  237. Conf_Init( );
  238. /* Initialize the "main program": chroot environment, user and
  239. * group ID, ... */
  240. if (!NGIRCd_Init(NGIRCd_NoDaemon)) {
  241. Log(LOG_ALERT, "Fatal: Initialization failed");
  242. exit(1);
  243. }
  244. /* Initialize modules, part II: these functions are eventually
  245. * called with already dropped privileges ... */
  246. Channel_Init( );
  247. Client_Init( );
  248. #ifdef ZEROCONF
  249. Rendezvous_Init( );
  250. #endif
  251. Conn_Init( );
  252. if (!io_library_init(CONNECTION_POOL)) {
  253. Log(LOG_ALERT, "Fatal: Cannot initialize IO routines: %s", strerror(errno));
  254. exit(1);
  255. }
  256. if (!Signals_Init()) {
  257. Log(LOG_ALERT, "Fatal: Could not set up signal handlers: %s", strerror(errno));
  258. exit(1);
  259. }
  260. /*
  261. * create protocol and server identification.
  262. * The syntax used by ngIRCd in PASS commands and the extended flags
  263. * are described in doc/Protocol.txt
  264. */
  265. #ifdef IRCPLUS
  266. snprintf( NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s:%s", PROTOVER, PROTOIRCPLUS, PACKAGE_NAME, PACKAGE_VERSION, IRCPLUSFLAGS );
  267. #ifdef ZLIB
  268. strcat( NGIRCd_ProtoID, "Z" );
  269. #endif
  270. if( Conf_OperCanMode ) strcat( NGIRCd_ProtoID, "o" );
  271. #else
  272. snprintf( NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s", PROTOVER, PROTOIRC, PACKAGE_NAME, PACKAGE_VERSION );
  273. #endif
  274. strlcat( NGIRCd_ProtoID, " P", sizeof NGIRCd_ProtoID );
  275. #ifdef ZLIB
  276. strlcat( NGIRCd_ProtoID, "Z", sizeof NGIRCd_ProtoID );
  277. #endif
  278. LogDebug("Protocol and server ID is \"%s\".", NGIRCd_ProtoID);
  279. Channel_InitPredefined( );
  280. if( Conn_InitListeners( ) < 1 )
  281. {
  282. Log( LOG_ALERT, "Server isn't listening on a single port!" );
  283. Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
  284. Pidfile_Delete( );
  285. exit( 1 );
  286. }
  287. /* Hauptschleife */
  288. Conn_Handler( );
  289. /* Alles abmelden */
  290. Conn_Exit( );
  291. #ifdef ZEROCONF
  292. Rendezvous_Exit( );
  293. #endif
  294. Client_Exit( );
  295. Channel_Exit( );
  296. Log_Exit( );
  297. }
  298. Pidfile_Delete( );
  299. return 0;
  300. } /* main */
  301. /**
  302. * Generate ngIRCd "version string".
  303. * This string is generated once and then stored in NGIRCd_Version for
  304. * further usage, for example by the IRC command VERSION and the --version
  305. * command line switch.
  306. */
  307. static void
  308. Fill_Version( void )
  309. {
  310. NGIRCd_VersionAddition[0] = '\0';
  311. #ifdef SYSLOG
  312. strlcpy( NGIRCd_VersionAddition, "SYSLOG", sizeof NGIRCd_VersionAddition );
  313. #endif
  314. #ifdef ZLIB
  315. if( NGIRCd_VersionAddition[0] )
  316. strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
  317. strlcat( NGIRCd_VersionAddition, "ZLIB", sizeof NGIRCd_VersionAddition );
  318. #endif
  319. #ifdef SSL_SUPPORT
  320. if ( NGIRCd_VersionAddition[0] ) strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
  321. strlcat( NGIRCd_VersionAddition, "SSL", sizeof NGIRCd_VersionAddition );
  322. #endif
  323. #ifdef TCPWRAP
  324. if( NGIRCd_VersionAddition[0] )
  325. strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
  326. strlcat( NGIRCd_VersionAddition, "TCPWRAP", sizeof NGIRCd_VersionAddition );
  327. #endif
  328. #ifdef ZEROCONF
  329. if( NGIRCd_VersionAddition[0] )
  330. strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
  331. strlcat( NGIRCd_VersionAddition, "ZEROCONF", sizeof NGIRCd_VersionAddition );
  332. #endif
  333. #ifdef IDENTAUTH
  334. if( NGIRCd_VersionAddition[0] )
  335. strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
  336. strlcat( NGIRCd_VersionAddition, "IDENT", sizeof NGIRCd_VersionAddition );
  337. #endif
  338. #ifdef PAM
  339. if (NGIRCd_VersionAddition[0])
  340. strlcat(NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition);
  341. strlcat(NGIRCd_VersionAddition, "PAM", sizeof NGIRCd_VersionAddition);
  342. #endif
  343. #ifdef DEBUG
  344. if( NGIRCd_VersionAddition[0] )
  345. strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
  346. strlcat( NGIRCd_VersionAddition, "DEBUG", sizeof NGIRCd_VersionAddition );
  347. #endif
  348. #ifdef SNIFFER
  349. if( NGIRCd_VersionAddition[0] )
  350. strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
  351. strlcat( NGIRCd_VersionAddition, "SNIFFER", sizeof NGIRCd_VersionAddition );
  352. #endif
  353. #ifdef STRICT_RFC
  354. if( NGIRCd_VersionAddition[0] )
  355. strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
  356. strlcat( NGIRCd_VersionAddition, "RFC", sizeof NGIRCd_VersionAddition );
  357. #endif
  358. #ifdef IRCPLUS
  359. if( NGIRCd_VersionAddition[0] )
  360. strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
  361. strlcat( NGIRCd_VersionAddition, "IRCPLUS", sizeof NGIRCd_VersionAddition );
  362. #endif
  363. #ifdef WANT_IPV6
  364. if (NGIRCd_VersionAddition[0])
  365. strlcat(NGIRCd_VersionAddition, "+", sizeof(NGIRCd_VersionAddition));
  366. strlcat(NGIRCd_VersionAddition, "IPv6", sizeof(NGIRCd_VersionAddition));
  367. #endif
  368. if( NGIRCd_VersionAddition[0] )
  369. strlcat( NGIRCd_VersionAddition, "-", sizeof( NGIRCd_VersionAddition ));
  370. strlcat( NGIRCd_VersionAddition, TARGET_CPU, sizeof( NGIRCd_VersionAddition ));
  371. strlcat( NGIRCd_VersionAddition, "/", sizeof( NGIRCd_VersionAddition ));
  372. strlcat( NGIRCd_VersionAddition, TARGET_VENDOR, sizeof( NGIRCd_VersionAddition ));
  373. strlcat( NGIRCd_VersionAddition, "/", sizeof( NGIRCd_VersionAddition ));
  374. strlcat( NGIRCd_VersionAddition, TARGET_OS, sizeof( NGIRCd_VersionAddition ));
  375. snprintf(NGIRCd_Version, sizeof NGIRCd_Version, "%s %s-%s",
  376. PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition);
  377. } /* Fill_Version */
  378. /**
  379. * Display copyright and version information of ngIRCd on the console.
  380. */
  381. static void
  382. Show_Version( void )
  383. {
  384. puts( NGIRCd_Version );
  385. puts( "Copyright (c)2001-2010 Alexander Barton (<alex@barton.de>) and Contributors." );
  386. puts( "Homepage: <http://ngircd.barton.de/>\n" );
  387. puts( "This is free software; see the source for copying conditions. There is NO" );
  388. puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
  389. } /* Show_Version */
  390. /**
  391. * Display a short help text on the console.
  392. * This help depends on the configuration of the executable and only shows
  393. * options that are actually enabled.
  394. */
  395. static void
  396. Show_Help( void )
  397. {
  398. #ifdef DEBUG
  399. puts( " -d, --debug log extra debug messages" );
  400. #endif
  401. puts( " -f, --config <f> use file <f> as configuration file" );
  402. puts( " -n, --nodaemon don't fork and don't detach from controlling terminal" );
  403. puts( " -p, --passive disable automatic connections to other servers" );
  404. #ifdef SNIFFER
  405. puts( " -s, --sniffer enable network sniffer and display all IRC traffic" );
  406. #endif
  407. puts( " -t, --configtest read, validate and display configuration; then exit" );
  408. puts( " -V, --version output version information and exit" );
  409. puts( " -h, --help display this help and exit" );
  410. } /* Show_Help */
  411. /**
  412. * Delete the file containing the process ID (PID).
  413. */
  414. static void
  415. Pidfile_Delete( void )
  416. {
  417. /* Pidfile configured? */
  418. if( ! Conf_PidFile[0] ) return;
  419. #ifdef DEBUG
  420. Log( LOG_DEBUG, "Removing PID file (%s) ...", Conf_PidFile );
  421. #endif
  422. if( unlink( Conf_PidFile ))
  423. Log( LOG_ERR, "Error unlinking PID file (%s): %s", Conf_PidFile, strerror( errno ));
  424. } /* Pidfile_Delete */
  425. /**
  426. * Create the file containing the process ID of ngIRCd ("PID file").
  427. * @param pid The process ID to be stored in this file.
  428. */
  429. static void
  430. Pidfile_Create(pid_t pid)
  431. {
  432. int pidfd;
  433. char pidbuf[64];
  434. int len;
  435. /* Pidfile configured? */
  436. if( ! Conf_PidFile[0] ) return;
  437. #ifdef DEBUG
  438. Log( LOG_DEBUG, "Creating PID file (%s) ...", Conf_PidFile );
  439. #endif
  440. pidfd = open( Conf_PidFile, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
  441. if ( pidfd < 0 ) {
  442. Log( LOG_ERR, "Error writing PID file (%s): %s", Conf_PidFile, strerror( errno ));
  443. return;
  444. }
  445. len = snprintf(pidbuf, sizeof pidbuf, "%ld\n", (long)pid);
  446. if (len < 0 || len >= (int)sizeof pidbuf) {
  447. Log( LOG_ERR, "Error converting pid");
  448. return;
  449. }
  450. if (write(pidfd, pidbuf, (size_t)len) != (ssize_t)len)
  451. Log( LOG_ERR, "Can't write PID file (%s): %s", Conf_PidFile, strerror( errno ));
  452. if( close(pidfd) != 0 )
  453. Log( LOG_ERR, "Error closing PID file (%s): %s", Conf_PidFile, strerror( errno ));
  454. } /* Pidfile_Create */
  455. /**
  456. * Redirect stdin, stdout and stderr to apropriate file handles.
  457. */
  458. static void
  459. Setup_FDStreams(int fd)
  460. {
  461. if (fd < 0)
  462. return;
  463. fflush(stdout);
  464. fflush(stderr);
  465. /* Create new stdin(0), stdout(1) and stderr(2) descriptors */
  466. dup2( fd, 0 ); dup2( fd, 1 ); dup2( fd, 2 );
  467. } /* Setup_FDStreams */
  468. static bool
  469. NGIRCd_getNobodyID(uid_t *uid, gid_t *gid )
  470. {
  471. struct passwd *pwd;
  472. #ifdef __CYGWIN__
  473. /* Cygwin kludge.
  474. * It can return EINVAL instead of EPERM
  475. * so, if we are already unprivileged,
  476. * use id of current user.
  477. */
  478. if (geteuid() && getuid()) {
  479. *uid = getuid();
  480. *gid = getgid();
  481. return true;
  482. }
  483. #endif
  484. pwd = getpwnam("nobody");
  485. if (!pwd) return false;
  486. if ( !pwd->pw_uid || !pwd->pw_gid)
  487. return false;
  488. *uid = pwd->pw_uid;
  489. *gid = pwd->pw_gid;
  490. endpwent();
  491. return true;
  492. }
  493. static bool
  494. NGIRCd_Init( bool NGIRCd_NoDaemon )
  495. {
  496. static bool initialized;
  497. bool chrooted = false;
  498. struct passwd *pwd;
  499. struct group *grp;
  500. int real_errno, fd = -1;
  501. pid_t pid;
  502. if (initialized)
  503. return true;
  504. if (!NGIRCd_NoDaemon) {
  505. /* open /dev/null before chroot() */
  506. fd = open( "/dev/null", O_RDWR);
  507. if (fd < 0)
  508. Log(LOG_WARNING, "Could not open /dev/null: %s", strerror(errno));
  509. }
  510. if (!ConnSSL_InitLibrary())
  511. Log(LOG_WARNING,
  512. "Warning: Error during SSL initialization, continuing ...");
  513. if( Conf_Chroot[0] ) {
  514. if( chdir( Conf_Chroot ) != 0 ) {
  515. Log( LOG_ERR, "Can't chdir() in ChrootDir (%s): %s", Conf_Chroot, strerror( errno ));
  516. goto out;
  517. }
  518. if( chroot( Conf_Chroot ) != 0 ) {
  519. if (errno != EPERM) {
  520. Log( LOG_ERR, "Can't change root directory to \"%s\": %s",
  521. Conf_Chroot, strerror( errno ));
  522. goto out;
  523. }
  524. } else {
  525. chrooted = true;
  526. Log( LOG_INFO, "Changed root and working directory to \"%s\".", Conf_Chroot );
  527. }
  528. }
  529. if (Conf_UID == 0) {
  530. Log(LOG_INFO, "ServerUID must not be 0, using \"nobody\" instead.", Conf_UID);
  531. if (! NGIRCd_getNobodyID(&Conf_UID, &Conf_GID)) {
  532. Log(LOG_WARNING, "Could not get user/group ID of user \"nobody\": %s",
  533. errno ? strerror(errno) : "not found" );
  534. goto out;
  535. }
  536. }
  537. if (getgid() != Conf_GID) {
  538. /* Change group ID */
  539. if (setgid(Conf_GID) != 0) {
  540. real_errno = errno;
  541. Log( LOG_ERR, "Can't change group ID to %u: %s", Conf_GID, strerror( errno ));
  542. if (real_errno != EPERM)
  543. goto out;
  544. }
  545. }
  546. if (getuid() != Conf_UID) {
  547. /* Change user ID */
  548. if (setuid(Conf_UID) != 0) {
  549. real_errno = errno;
  550. Log(LOG_ERR, "Can't change user ID to %u: %s", Conf_UID, strerror(errno));
  551. if (real_errno != EPERM)
  552. goto out;
  553. }
  554. }
  555. initialized = true;
  556. /* Normally a child process is forked which isn't any longer
  557. * connected to ther controlling terminal. Use "--nodaemon"
  558. * to disable this "daemon mode" (useful for debugging). */
  559. if ( ! NGIRCd_NoDaemon ) {
  560. pid = fork( );
  561. if( pid > 0 ) {
  562. /* "Old" process: exit. */
  563. exit( 0 );
  564. }
  565. if( pid < 0 ) {
  566. /* Error!? */
  567. fprintf( stderr, "%s: Can't fork: %s!\nFatal error, exiting now ...\n",
  568. PACKAGE_NAME, strerror( errno ));
  569. exit( 1 );
  570. }
  571. /* New child process */
  572. #ifndef NeXT
  573. (void)setsid( );
  574. #else
  575. setpgrp(0, getpid());
  576. #endif
  577. if (chdir( "/" ) != 0)
  578. Log(LOG_ERR, "Can't change directory to '/': %s",
  579. strerror(errno));
  580. /* Detach stdin, stdout and stderr */
  581. Setup_FDStreams(fd);
  582. if (fd > 2) {
  583. close(fd);
  584. fd = -1;
  585. }
  586. }
  587. pid = getpid();
  588. Pidfile_Create( pid );
  589. /* Check UID/GID we are running as, can be different from values
  590. * configured (e. g. if we were already started with a UID>0. */
  591. Conf_UID = getuid();
  592. Conf_GID = getgid();
  593. pwd = getpwuid( Conf_UID );
  594. grp = getgrgid( Conf_GID );
  595. Log(LOG_INFO, "Running as user %s(%ld), group %s(%ld), with PID %ld.",
  596. pwd ? pwd->pw_name : "unknown", (long)Conf_UID,
  597. grp ? grp->gr_name : "unknown", (long)Conf_GID, (long)pid);
  598. if (chrooted) {
  599. Log(LOG_INFO, "Running with root directory \"%s\".",
  600. Conf_Chroot );
  601. return true;
  602. } else
  603. Log(LOG_INFO, "Not running with changed root directory.");
  604. /* Change working directory to home directory of the user
  605. * we are running as (only when running in daemon mode and not in chroot) */
  606. if (pwd) {
  607. if (!NGIRCd_NoDaemon ) {
  608. if( chdir( pwd->pw_dir ) == 0 )
  609. Log( LOG_DEBUG, "Changed working directory to \"%s\" ...", pwd->pw_dir );
  610. else
  611. Log( LOG_INFO, "Notice: Can't change working directory to \"%s\": %s",
  612. pwd->pw_dir, strerror( errno ));
  613. }
  614. } else {
  615. Log( LOG_ERR, "Can't get user informaton for UID %d!?", Conf_UID );
  616. }
  617. return true;
  618. out:
  619. if (fd > 2)
  620. close(fd);
  621. return false;
  622. }
  623. /* -eof- */