ngircd.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2007 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. static char UNUSED id[] = "$Id: ngircd.c,v 1.113.2.2 2007/05/05 20:25:47 alex Exp $";
  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 "imp.h"
  19. #include <assert.h>
  20. #include <errno.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <signal.h>
  24. #include <string.h>
  25. #include <unistd.h>
  26. #include <time.h>
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #include <sys/wait.h>
  30. #include <fcntl.h>
  31. #include <pwd.h>
  32. #include <grp.h>
  33. #include "defines.h"
  34. #include "resolve.h"
  35. #include "conn.h"
  36. #include "client.h"
  37. #include "channel.h"
  38. #include "conf.h"
  39. #include "cvs-version.h"
  40. #include "lists.h"
  41. #include "log.h"
  42. #include "parse.h"
  43. #include "irc.h"
  44. #ifdef ZEROCONF
  45. #include "rendezvous.h"
  46. #endif
  47. #include "exp.h"
  48. #include "ngircd.h"
  49. static void Initialize_Signal_Handler PARAMS(( void ));
  50. static void Signal_Handler PARAMS(( int Signal ));
  51. static void Show_Version PARAMS(( void ));
  52. static void Show_Help PARAMS(( void ));
  53. static void Pidfile_Create PARAMS(( pid_t pid ));
  54. static void Pidfile_Delete PARAMS(( void ));
  55. static void Fill_Version PARAMS(( void ));
  56. static void Setup_FDStreams PARAMS(( void ));
  57. static bool NGIRCd_Init PARAMS(( bool ));
  58. /**
  59. * The main() function of ngIRCd.
  60. * Here all starts: this function is called by the operating system loader,
  61. * it is the first portion of code executed of ngIRCd.
  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. umask( 0077 );
  74. NGIRCd_SignalQuit = NGIRCd_SignalRestart = NGIRCd_SignalRehash = false;
  75. NGIRCd_Passive = false;
  76. #ifdef DEBUG
  77. NGIRCd_Debug = false;
  78. #endif
  79. #ifdef SNIFFER
  80. NGIRCd_Sniffer = false;
  81. #endif
  82. strlcpy( NGIRCd_ConfFile, SYSCONFDIR, sizeof( NGIRCd_ConfFile ));
  83. strlcat( NGIRCd_ConfFile, CONFIG_FILE, sizeof( NGIRCd_ConfFile ));
  84. Fill_Version( );
  85. /* Kommandozeile parsen */
  86. for( i = 1; i < argc; i++ )
  87. {
  88. ok = false;
  89. if(( argv[i][0] == '-' ) && ( argv[i][1] == '-' ))
  90. {
  91. /* Lange Option */
  92. if( strcmp( argv[i], "--config" ) == 0 )
  93. {
  94. if( i + 1 < argc )
  95. {
  96. /* Ok, there's an parameter left */
  97. strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile ));
  98. /* next parameter */
  99. i++; ok = true;
  100. }
  101. }
  102. if( strcmp( argv[i], "--configtest" ) == 0 )
  103. {
  104. configtest = true;
  105. ok = true;
  106. }
  107. #ifdef DEBUG
  108. if( strcmp( argv[i], "--debug" ) == 0 )
  109. {
  110. NGIRCd_Debug = true;
  111. ok = true;
  112. }
  113. #endif
  114. if( strcmp( argv[i], "--help" ) == 0 )
  115. {
  116. Show_Version( );
  117. puts( "" ); Show_Help( ); puts( "" );
  118. exit( 1 );
  119. }
  120. if( strcmp( argv[i], "--nodaemon" ) == 0 )
  121. {
  122. NGIRCd_NoDaemon = true;
  123. ok = true;
  124. }
  125. if( strcmp( argv[i], "--passive" ) == 0 )
  126. {
  127. NGIRCd_Passive = true;
  128. ok = true;
  129. }
  130. #ifdef SNIFFER
  131. if( strcmp( argv[i], "--sniffer" ) == 0 )
  132. {
  133. NGIRCd_Sniffer = true;
  134. ok = true;
  135. }
  136. #endif
  137. if( strcmp( argv[i], "--version" ) == 0 )
  138. {
  139. Show_Version( );
  140. exit( 1 );
  141. }
  142. }
  143. else if(( argv[i][0] == '-' ) && ( argv[i][1] != '-' ))
  144. {
  145. /* Kurze Option */
  146. for( n = 1; n < strlen( argv[i] ); n++ )
  147. {
  148. ok = false;
  149. #ifdef DEBUG
  150. if( argv[i][n] == 'd' )
  151. {
  152. NGIRCd_Debug = true;
  153. ok = true;
  154. }
  155. #endif
  156. if( argv[i][n] == 'f' )
  157. {
  158. if(( ! argv[i][n + 1] ) && ( i + 1 < argc ))
  159. {
  160. /* Ok, next character is a blank */
  161. strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile ));
  162. /* go to the following parameter */
  163. i++;
  164. n = strlen( argv[i] );
  165. ok = true;
  166. }
  167. }
  168. if( argv[i][n] == 'n' )
  169. {
  170. NGIRCd_NoDaemon = true;
  171. ok = true;
  172. }
  173. if( argv[i][n] == 'p' )
  174. {
  175. NGIRCd_Passive = true;
  176. ok = true;
  177. }
  178. #ifdef SNIFFER
  179. if( argv[i][n] == 's' )
  180. {
  181. NGIRCd_Sniffer = true;
  182. ok = true;
  183. }
  184. #endif
  185. if( argv[i][n] == 't' )
  186. {
  187. configtest = true;
  188. ok = true;
  189. }
  190. if( ! ok )
  191. {
  192. printf( "%s: invalid option \"-%c\"!\n", PACKAGE_NAME, argv[i][n] );
  193. printf( "Try \"%s --help\" for more information.\n", PACKAGE_NAME );
  194. exit( 1 );
  195. }
  196. }
  197. }
  198. if( ! ok )
  199. {
  200. printf( "%s: invalid option \"%s\"!\n", PACKAGE_NAME, argv[i] );
  201. printf( "Try \"%s --help\" for more information.\n", PACKAGE_NAME );
  202. exit( 1 );
  203. }
  204. }
  205. /* Debug-Level (fuer IRC-Befehl "VERSION") ermitteln */
  206. NGIRCd_DebugLevel[0] = '\0';
  207. #ifdef DEBUG
  208. if( NGIRCd_Debug ) strcpy( NGIRCd_DebugLevel, "1" );
  209. #endif
  210. #ifdef SNIFFER
  211. if( NGIRCd_Sniffer )
  212. {
  213. NGIRCd_Debug = true;
  214. strcpy( NGIRCd_DebugLevel, "2" );
  215. }
  216. #endif
  217. /* Soll nur die Konfigurations ueberprueft und ausgegeben werden? */
  218. if( configtest )
  219. {
  220. Show_Version( ); puts( "" );
  221. exit( Conf_Test( ));
  222. }
  223. while( ! NGIRCd_SignalQuit )
  224. {
  225. /* Initialize global variables */
  226. NGIRCd_Start = time( NULL );
  227. (void)strftime( NGIRCd_StartStr, 64, "%a %b %d %Y at %H:%M:%S (%Z)", localtime( &NGIRCd_Start ));
  228. NGIRCd_SignalRehash = false;
  229. NGIRCd_SignalRestart = false;
  230. NGIRCd_SignalQuit = false;
  231. /* Initialize modules, part I */
  232. Log_Init( ! NGIRCd_NoDaemon );
  233. Conf_Init( );
  234. /* Initialize the "main program": chroot environment, user and
  235. * group ID, ... */
  236. if (!NGIRCd_Init(NGIRCd_NoDaemon)) {
  237. Log(LOG_ALERT, "Fatal: Initialization failed");
  238. exit(1);
  239. }
  240. /* Initialize modules, part II: these functions are eventually
  241. * called with already dropped privileges ... */
  242. Channel_Init( );
  243. Client_Init( );
  244. #ifdef ZEROCONF
  245. Rendezvous_Init( );
  246. #endif
  247. Conn_Init( );
  248. #ifdef DEBUG
  249. /* Redirect stderr handle to "error file" for debugging
  250. * when not running in "no daemon" mode: */
  251. if( ! NGIRCd_NoDaemon ) Log_InitErrorfile( );
  252. #endif
  253. /* Signal-Handler initialisieren */
  254. Initialize_Signal_Handler( );
  255. /* Protokoll- und Server-Identifikation erzeugen. Die vom ngIRCd
  256. * beim PASS-Befehl verwendete Syntax sowie die erweiterten Flags
  257. * sind in doc/Protocol.txt beschrieben. */
  258. #ifdef IRCPLUS
  259. snprintf( NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s:%s", PROTOVER, PROTOIRCPLUS, PACKAGE_NAME, PACKAGE_VERSION, IRCPLUSFLAGS );
  260. #ifdef ZLIB
  261. strcat( NGIRCd_ProtoID, "Z" );
  262. #endif
  263. if( Conf_OperCanMode ) strcat( NGIRCd_ProtoID, "o" );
  264. #else
  265. snprintf( NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s", PROTOVER, PROTOIRC, PACKAGE_NAME, PACKAGE_VERSION );
  266. #endif
  267. strlcat( NGIRCd_ProtoID, " P", sizeof NGIRCd_ProtoID );
  268. #ifdef ZLIB
  269. strlcat( NGIRCd_ProtoID, "Z", sizeof NGIRCd_ProtoID );
  270. #endif
  271. Log( LOG_DEBUG, "Protocol and server ID is \"%s\".", NGIRCd_ProtoID );
  272. /* Vordefinierte Channels anlegen */
  273. Channel_InitPredefined( );
  274. /* Listen-Ports initialisieren */
  275. if( Conn_InitListeners( ) < 1 )
  276. {
  277. Log( LOG_ALERT, "Server isn't listening on a single port!" );
  278. Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
  279. Pidfile_Delete( );
  280. exit( 1 );
  281. }
  282. /* Hauptschleife */
  283. Conn_Handler( );
  284. /* Alles abmelden */
  285. Conn_Exit( );
  286. #ifdef ZEROCONF
  287. Rendezvous_Exit( );
  288. #endif
  289. Client_Exit( );
  290. Channel_Exit( );
  291. Log_Exit( );
  292. }
  293. Pidfile_Delete( );
  294. return 0;
  295. } /* main */
  296. /**
  297. * Generate ngIRCd "version string".
  298. * This string is generated once and then stored in NGIRCd_Version for
  299. * further usage, for example by the IRC command VERSION and the --version
  300. * command line switch.
  301. */
  302. static void
  303. Fill_Version( void )
  304. {
  305. NGIRCd_VersionAddition[0] = '\0';
  306. #ifdef SYSLOG
  307. strlcpy( NGIRCd_VersionAddition, "SYSLOG", sizeof NGIRCd_VersionAddition );
  308. #endif
  309. #ifdef ZLIB
  310. if( NGIRCd_VersionAddition[0] )
  311. strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
  312. strlcat( NGIRCd_VersionAddition, "ZLIB", sizeof NGIRCd_VersionAddition );
  313. #endif
  314. #ifdef TCPWRAP
  315. if( NGIRCd_VersionAddition[0] )
  316. strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
  317. strlcat( NGIRCd_VersionAddition, "TCPWRAP", sizeof NGIRCd_VersionAddition );
  318. #endif
  319. #ifdef ZEROCONF
  320. if( NGIRCd_VersionAddition[0] )
  321. strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
  322. strlcat( NGIRCd_VersionAddition, "ZEROCONF", sizeof NGIRCd_VersionAddition );
  323. #endif
  324. #ifdef IDENTAUTH
  325. if( NGIRCd_VersionAddition[0] )
  326. strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
  327. strlcat( NGIRCd_VersionAddition, "IDENT", sizeof NGIRCd_VersionAddition );
  328. #endif
  329. #ifdef DEBUG
  330. if( NGIRCd_VersionAddition[0] )
  331. strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
  332. strlcat( NGIRCd_VersionAddition, "DEBUG", sizeof NGIRCd_VersionAddition );
  333. #endif
  334. #ifdef SNIFFER
  335. if( NGIRCd_VersionAddition[0] )
  336. strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
  337. strlcat( NGIRCd_VersionAddition, "SNIFFER", sizeof NGIRCd_VersionAddition );
  338. #endif
  339. #ifdef STRICT_RFC
  340. if( NGIRCd_VersionAddition[0] )
  341. strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
  342. strlcat( NGIRCd_VersionAddition, "RFC", sizeof NGIRCd_VersionAddition );
  343. #endif
  344. #ifdef IRCPLUS
  345. if( NGIRCd_VersionAddition[0] )
  346. strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
  347. strlcat( NGIRCd_VersionAddition, "IRCPLUS", sizeof NGIRCd_VersionAddition );
  348. #endif
  349. if( NGIRCd_VersionAddition[0] )
  350. strlcat( NGIRCd_VersionAddition, "-", sizeof( NGIRCd_VersionAddition ));
  351. strlcat( NGIRCd_VersionAddition, TARGET_CPU, sizeof( NGIRCd_VersionAddition ));
  352. strlcat( NGIRCd_VersionAddition, "/", sizeof( NGIRCd_VersionAddition ));
  353. strlcat( NGIRCd_VersionAddition, TARGET_VENDOR, sizeof( NGIRCd_VersionAddition ));
  354. strlcat( NGIRCd_VersionAddition, "/", sizeof( NGIRCd_VersionAddition ));
  355. strlcat( NGIRCd_VersionAddition, TARGET_OS, sizeof( NGIRCd_VersionAddition ));
  356. #ifdef CVSDATE
  357. snprintf( NGIRCd_Version, sizeof NGIRCd_Version,"%s %s(%s)-%s", PACKAGE_NAME, PACKAGE_VERSION, CVSDATE, NGIRCd_VersionAddition);
  358. #else
  359. snprintf( NGIRCd_Version, sizeof NGIRCd_Version, "%s %s-%s", PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition);
  360. #endif
  361. } /* Fill_Version */
  362. /**
  363. * Reload the server configuration file.
  364. */
  365. GLOBAL void
  366. NGIRCd_Rehash( void )
  367. {
  368. char old_name[CLIENT_ID_LEN];
  369. Log( LOG_NOTICE|LOG_snotice, "Re-reading configuration NOW!" );
  370. NGIRCd_SignalRehash = false;
  371. /* Close down all listening sockets */
  372. Conn_ExitListeners( );
  373. /* Remember old server name */
  374. strlcpy( old_name, Conf_ServerName, sizeof old_name );
  375. /* Re-read configuration ... */
  376. Conf_Rehash( );
  377. /* Recover old server name: it can't be changed during run-time */
  378. if( strcmp( old_name, Conf_ServerName ) != 0 )
  379. {
  380. strlcpy( Conf_ServerName, old_name, sizeof Conf_ServerName );
  381. Log( LOG_ERR, "Can't change \"ServerName\" on runtime! Ignored new name." );
  382. }
  383. /* Create new pre-defined channels */
  384. Channel_InitPredefined( );
  385. /* Start listening on sockets */
  386. Conn_InitListeners( );
  387. /* Sync configuration with established connections */
  388. Conn_SyncServerStruct( );
  389. Log( LOG_NOTICE|LOG_snotice, "Re-reading of configuration done." );
  390. } /* NGIRCd_Rehash */
  391. /**
  392. * Initialize the signal handler.
  393. */
  394. static void
  395. Initialize_Signal_Handler( void )
  396. {
  397. /* Signal-Handler initialisieren: einige Signale
  398. * werden ignoriert, andere speziell behandelt. */
  399. #ifdef HAVE_SIGACTION
  400. /* sigaction() ist vorhanden */
  401. struct sigaction saction;
  402. /* Signal-Struktur initialisieren */
  403. memset( &saction, 0, sizeof( saction ));
  404. saction.sa_handler = Signal_Handler;
  405. #ifdef SA_RESTART
  406. saction.sa_flags |= SA_RESTART;
  407. #endif
  408. #ifdef SA_NOCLDWAIT
  409. saction.sa_flags |= SA_NOCLDWAIT;
  410. #endif
  411. /* Signal-Handler einhaengen */
  412. sigaction(SIGINT, &saction, NULL);
  413. sigaction(SIGQUIT, &saction, NULL);
  414. sigaction(SIGTERM, &saction, NULL);
  415. sigaction(SIGHUP, &saction, NULL);
  416. sigaction(SIGCHLD, &saction, NULL);
  417. /* einige Signale ignorieren */
  418. saction.sa_handler = SIG_IGN;
  419. sigaction(SIGPIPE, &saction, NULL);
  420. #else
  421. /* kein sigaction() vorhanden */
  422. /* Signal-Handler einhaengen */
  423. signal(SIGINT, Signal_Handler);
  424. signal(SIGQUIT, Signal_Handler);
  425. signal(SIGTERM, Signal_Handler);
  426. signal(SIGHUP, Signal_Handler);
  427. signal(SIGCHLD, Signal_Handler);
  428. /* einige Signale ignorieren */
  429. signal(SIGPIPE, SIG_IGN);
  430. #endif
  431. } /* Initialize_Signal_Handler */
  432. /**
  433. * Signal handler of ngIRCd.
  434. * This function is called whenever ngIRCd catches a signal sent by the
  435. * user and/or the system to it. For example SIGTERM and SIGHUP.
  436. * @param Signal Number of the signal to handle.
  437. */
  438. static void
  439. Signal_Handler( int Signal )
  440. {
  441. switch( Signal )
  442. {
  443. case SIGTERM:
  444. case SIGINT:
  445. case SIGQUIT:
  446. /* wir soll(t)en uns wohl beenden ... */
  447. NGIRCd_SignalQuit = true;
  448. break;
  449. case SIGHUP:
  450. /* Konfiguration neu einlesen: */
  451. NGIRCd_SignalRehash = true;
  452. break;
  453. case SIGCHLD:
  454. /* Child-Prozess wurde beendet. Zombies vermeiden: */
  455. while( waitpid( -1, NULL, WNOHANG ) > 0);
  456. break;
  457. #ifdef DEBUG
  458. default:
  459. /* unbekanntes bzw. unbehandeltes Signal */
  460. Log( LOG_DEBUG, "Got signal %d! Ignored.", Signal );
  461. #endif
  462. }
  463. } /* Signal_Handler */
  464. /**
  465. * Display copyright and version information of ngIRCd on the console.
  466. */
  467. static void
  468. Show_Version( void )
  469. {
  470. puts( NGIRCd_Version );
  471. puts( "Copyright (c)2001-2007 Alexander Barton (<alex@barton.de>) and Contributors." );
  472. puts( "Homepage: <http://ngircd.barton.de/>\n" );
  473. puts( "This is free software; see the source for copying conditions. There is NO" );
  474. puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
  475. } /* Show_Version */
  476. /**
  477. * Display a short help text on the console.
  478. * This help depends on the configuration of the executable and only shows
  479. * options that are actually enabled.
  480. */
  481. static void
  482. Show_Help( void )
  483. {
  484. #ifdef DEBUG
  485. puts( " -d, --debug log extra debug messages" );
  486. #endif
  487. puts( " -f, --config <f> use file <f> as configuration file" );
  488. puts( " -n, --nodaemon don't fork and don't detach from controlling terminal" );
  489. puts( " -p, --passive disable automatic connections to other servers" );
  490. #ifdef SNIFFER
  491. puts( " -s, --sniffer enable network sniffer and display all IRC traffic" );
  492. #endif
  493. puts( " -t, --configtest read, validate and display configuration; then exit" );
  494. puts( " --version output version information and exit" );
  495. puts( " --help display this help and exit" );
  496. } /* Show_Help */
  497. /**
  498. * Delete the file containing the process ID (PID).
  499. */
  500. static void
  501. Pidfile_Delete( void )
  502. {
  503. /* Pidfile configured? */
  504. if( ! Conf_PidFile[0] ) return;
  505. #ifdef DEBUG
  506. Log( LOG_DEBUG, "Removing PID file (%s) ...", Conf_PidFile );
  507. #endif
  508. if( unlink( Conf_PidFile ))
  509. Log( LOG_ERR, "Error unlinking PID file (%s): %s", Conf_PidFile, strerror( errno ));
  510. } /* Pidfile_Delete */
  511. /**
  512. * Create the file containing the process ID of ngIRCd ("PID file").
  513. * @param pid The process ID to be stored in this file.
  514. */
  515. static void
  516. Pidfile_Create(pid_t pid)
  517. {
  518. int pidfd;
  519. char pidbuf[64];
  520. int len;
  521. /* Pidfile configured? */
  522. if( ! Conf_PidFile[0] ) return;
  523. #ifdef DEBUG
  524. Log( LOG_DEBUG, "Creating PID file (%s) ...", Conf_PidFile );
  525. #endif
  526. pidfd = open( Conf_PidFile, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
  527. if ( pidfd < 0 ) {
  528. Log( LOG_ERR, "Error writing PID file (%s): %s", Conf_PidFile, strerror( errno ));
  529. return;
  530. }
  531. len = snprintf(pidbuf, sizeof pidbuf, "%ld\n", (long)pid);
  532. if (len < 0 || len >= (int)sizeof pidbuf) {
  533. Log( LOG_ERR, "Error converting pid");
  534. return;
  535. }
  536. if (write(pidfd, pidbuf, (size_t)len) != (ssize_t)len)
  537. Log( LOG_ERR, "Can't write PID file (%s): %s", Conf_PidFile, strerror( errno ));
  538. if( close(pidfd) != 0 )
  539. Log( LOG_ERR, "Error closing PID file (%s): %s", Conf_PidFile, strerror( errno ));
  540. } /* Pidfile_Create */
  541. /**
  542. * Redirect stdin, stdout and stderr to apropriate file handles.
  543. */
  544. static void
  545. Setup_FDStreams( void )
  546. {
  547. int fd;
  548. /* Test if we can open /dev/null for reading and writing. If not
  549. * we are most probably chrooted already and the server has been
  550. * restarted. So we simply don't try to redirect stdXXX ... */
  551. fd = open( "/dev/null", O_RDWR );
  552. if ( fd < 0 ) {
  553. Log(LOG_WARNING, "Could not open /dev/null: %s", strerror(errno));
  554. return;
  555. }
  556. fflush(stdout);
  557. fflush(stderr);
  558. /* Create new stdin(0), stdout(1) and stderr(2) descriptors */
  559. dup2( fd, 0 ); dup2( fd, 1 ); dup2( fd, 2 );
  560. /* Close newly opened file descriptor if not stdin/out/err */
  561. if( fd > 2 ) close( fd );
  562. } /* Setup_FDStreams */
  563. static bool
  564. NGIRCd_getNobodyID(uid_t *uid, gid_t *gid )
  565. {
  566. struct passwd *pwd;
  567. pwd = getpwnam("nobody");
  568. if (!pwd) return false;
  569. if ( !pwd->pw_uid || !pwd->pw_gid)
  570. return false;
  571. *uid = pwd->pw_uid;
  572. *gid = pwd->pw_gid;
  573. endpwent();
  574. return true;
  575. }
  576. static bool
  577. NGIRCd_Init( bool NGIRCd_NoDaemon )
  578. {
  579. static bool initialized;
  580. bool chrooted = false;
  581. struct passwd *pwd;
  582. struct group *grp;
  583. int real_errno;
  584. pid_t pid;
  585. if (initialized)
  586. return true;
  587. if( Conf_Chroot[0] ) {
  588. if( chdir( Conf_Chroot ) != 0 ) {
  589. Log( LOG_ERR, "Can't chdir() in ChrootDir (%s): %s", Conf_Chroot, strerror( errno ));
  590. return false;
  591. }
  592. if( chroot( Conf_Chroot ) != 0 ) {
  593. if (errno != EPERM) {
  594. Log( LOG_ERR, "Can't change root directory to \"%s\": %s",
  595. Conf_Chroot, strerror( errno ));
  596. return false;
  597. }
  598. } else {
  599. chrooted = true;
  600. Log( LOG_INFO, "Changed root and working directory to \"%s\".", Conf_Chroot );
  601. }
  602. }
  603. if (Conf_UID == 0) {
  604. Log(LOG_INFO, "ServerUID must not be 0, using \"nobody\" instead.", Conf_UID);
  605. if (! NGIRCd_getNobodyID(&Conf_UID, &Conf_GID)) {
  606. Log(LOG_WARNING, "Could not get user/group ID of user \"nobody\": %s",
  607. errno ? strerror(errno) : "not found" );
  608. return false;
  609. }
  610. }
  611. if (getgid() != Conf_GID) {
  612. /* Change group ID */
  613. if (setgid(Conf_GID) != 0) {
  614. real_errno = errno;
  615. Log( LOG_ERR, "Can't change group ID to %u: %s", Conf_GID, strerror( errno ));
  616. if (real_errno != EPERM)
  617. return false;
  618. }
  619. }
  620. if (getuid() != Conf_UID) {
  621. /* Change user ID */
  622. if (setuid(Conf_UID) != 0) {
  623. real_errno = errno;
  624. Log(LOG_ERR, "Can't change user ID to %u: %s", Conf_UID, strerror(errno));
  625. if (real_errno != EPERM)
  626. return false;
  627. }
  628. }
  629. initialized = true;
  630. /* Normally a child process is forked which isn't any longer
  631. * connected to ther controlling terminal. Use "--nodaemon"
  632. * to disable this "daemon mode" (useful for debugging). */
  633. if ( ! NGIRCd_NoDaemon ) {
  634. pid = fork( );
  635. if( pid > 0 ) {
  636. /* "Old" process: exit. */
  637. exit( 0 );
  638. }
  639. if( pid < 0 ) {
  640. /* Error!? */
  641. fprintf( stderr, "%s: Can't fork: %s!\nFatal error, exiting now ...\n",
  642. PACKAGE_NAME, strerror( errno ));
  643. exit( 1 );
  644. }
  645. /* New child process */
  646. (void)setsid( );
  647. chdir( "/" );
  648. /* Detach stdin, stdout and stderr */
  649. Setup_FDStreams( );
  650. }
  651. pid = getpid();
  652. Pidfile_Create( pid );
  653. /* Check UID/GID we are running as, can be different from values
  654. * configured (e. g. if we were already started with a UID>0. */
  655. Conf_UID = getuid();
  656. Conf_GID = getgid();
  657. pwd = getpwuid( Conf_UID );
  658. grp = getgrgid( Conf_GID );
  659. Log( LOG_INFO, "Running as user %s(%ld), group %s(%ld), with PID %ld.",
  660. pwd ? pwd->pw_name : "unknown", Conf_UID,
  661. grp ? grp->gr_name : "unknown", Conf_GID, pid);
  662. if ( chrooted ) {
  663. Log( LOG_INFO, "Running chrooted, chrootdir \"%s\".", Conf_Chroot );
  664. return true;
  665. } else {
  666. Log( LOG_INFO, "Not running chrooted." );
  667. }
  668. /* Change working directory to home directory of the user
  669. * we are running as (only when running in daemon mode and not in chroot) */
  670. if ( pwd ) {
  671. if (!NGIRCd_NoDaemon ) {
  672. if( chdir( pwd->pw_dir ) == 0 )
  673. Log( LOG_DEBUG, "Changed working directory to \"%s\" ...", pwd->pw_dir );
  674. else
  675. Log( LOG_ERR, "Can't change working directory to \"%s\": %s",
  676. pwd->pw_dir, strerror( errno ));
  677. }
  678. } else {
  679. Log( LOG_ERR, "Can't get user informaton for UID %d!?", Conf_UID );
  680. }
  681. return true;
  682. }
  683. /* -eof- */