ngircd.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2006 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 2006/07/23 12:07:33 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. Lists_Init( );
  243. Channel_Init( );
  244. Client_Init( );
  245. #ifdef ZEROCONF
  246. Rendezvous_Init( );
  247. #endif
  248. Conn_Init( );
  249. #ifdef DEBUG
  250. /* Redirect stderr handle to "error file" for debugging
  251. * when not running in "no daemon" mode: */
  252. if( ! NGIRCd_NoDaemon ) Log_InitErrorfile( );
  253. #endif
  254. /* Signal-Handler initialisieren */
  255. Initialize_Signal_Handler( );
  256. /* Protokoll- und Server-Identifikation erzeugen. Die vom ngIRCd
  257. * beim PASS-Befehl verwendete Syntax sowie die erweiterten Flags
  258. * sind in doc/Protocol.txt beschrieben. */
  259. #ifdef IRCPLUS
  260. snprintf( NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s:%s", PROTOVER, PROTOIRCPLUS, PACKAGE_NAME, PACKAGE_VERSION, IRCPLUSFLAGS );
  261. #ifdef ZLIB
  262. strcat( NGIRCd_ProtoID, "Z" );
  263. #endif
  264. if( Conf_OperCanMode ) strcat( NGIRCd_ProtoID, "o" );
  265. #else
  266. snprintf( NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s", PROTOVER, PROTOIRC, PACKAGE_NAME, PACKAGE_VERSION );
  267. #endif
  268. strlcat( NGIRCd_ProtoID, " P", sizeof NGIRCd_ProtoID );
  269. #ifdef ZLIB
  270. strlcat( NGIRCd_ProtoID, "Z", sizeof NGIRCd_ProtoID );
  271. #endif
  272. Log( LOG_DEBUG, "Protocol and server ID is \"%s\".", NGIRCd_ProtoID );
  273. /* Vordefinierte Channels anlegen */
  274. Channel_InitPredefined( );
  275. /* Listen-Ports initialisieren */
  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. /* Hauptschleife */
  284. Conn_Handler( );
  285. /* Alles abmelden */
  286. Conn_Exit( );
  287. #ifdef ZEROCONF
  288. Rendezvous_Exit( );
  289. #endif
  290. Client_Exit( );
  291. Channel_Exit( );
  292. Lists_Exit( );
  293. Log_Exit( );
  294. }
  295. Pidfile_Delete( );
  296. return 0;
  297. } /* main */
  298. /**
  299. * Generate ngIRCd "version string".
  300. * This string is generated once and then stored in NGIRCd_Version for
  301. * further usage, for example by the IRC command VERSION and the --version
  302. * command line switch.
  303. */
  304. static void
  305. Fill_Version( void )
  306. {
  307. NGIRCd_VersionAddition[0] = '\0';
  308. #ifdef SYSLOG
  309. strlcpy( NGIRCd_VersionAddition, "SYSLOG", sizeof NGIRCd_VersionAddition );
  310. #endif
  311. #ifdef ZLIB
  312. if( NGIRCd_VersionAddition[0] )
  313. strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
  314. strlcat( NGIRCd_VersionAddition, "ZLIB", sizeof NGIRCd_VersionAddition );
  315. #endif
  316. #ifdef TCPWRAP
  317. if( NGIRCd_VersionAddition[0] )
  318. strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
  319. strlcat( NGIRCd_VersionAddition, "TCPWRAP", sizeof NGIRCd_VersionAddition );
  320. #endif
  321. #ifdef ZEROCONF
  322. if( NGIRCd_VersionAddition[0] )
  323. strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
  324. strlcat( NGIRCd_VersionAddition, "ZEROCONF", 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 DEBUG
  332. if( NGIRCd_VersionAddition[0] )
  333. strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
  334. strlcat( NGIRCd_VersionAddition, "DEBUG", sizeof NGIRCd_VersionAddition );
  335. #endif
  336. #ifdef SNIFFER
  337. if( NGIRCd_VersionAddition[0] )
  338. strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
  339. strlcat( NGIRCd_VersionAddition, "SNIFFER", sizeof NGIRCd_VersionAddition );
  340. #endif
  341. #ifdef STRICT_RFC
  342. if( NGIRCd_VersionAddition[0] )
  343. strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
  344. strlcat( NGIRCd_VersionAddition, "RFC", sizeof NGIRCd_VersionAddition );
  345. #endif
  346. #ifdef IRCPLUS
  347. if( NGIRCd_VersionAddition[0] )
  348. strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
  349. strlcat( NGIRCd_VersionAddition, "IRCPLUS", sizeof NGIRCd_VersionAddition );
  350. #endif
  351. if( NGIRCd_VersionAddition[0] )
  352. strlcat( NGIRCd_VersionAddition, "-", sizeof( NGIRCd_VersionAddition ));
  353. strlcat( NGIRCd_VersionAddition, TARGET_CPU, sizeof( NGIRCd_VersionAddition ));
  354. strlcat( NGIRCd_VersionAddition, "/", sizeof( NGIRCd_VersionAddition ));
  355. strlcat( NGIRCd_VersionAddition, TARGET_VENDOR, sizeof( NGIRCd_VersionAddition ));
  356. strlcat( NGIRCd_VersionAddition, "/", sizeof( NGIRCd_VersionAddition ));
  357. strlcat( NGIRCd_VersionAddition, TARGET_OS, sizeof( NGIRCd_VersionAddition ));
  358. #ifdef CVSDATE
  359. snprintf( NGIRCd_Version, sizeof NGIRCd_Version,"%s %s(%s)-%s", PACKAGE_NAME, PACKAGE_VERSION, CVSDATE, NGIRCd_VersionAddition);
  360. #else
  361. snprintf( NGIRCd_Version, sizeof NGIRCd_Version, "%s %s-%s", PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition);
  362. #endif
  363. } /* Fill_Version */
  364. /**
  365. * Reload the server configuration file.
  366. */
  367. GLOBAL void
  368. NGIRCd_Rehash( void )
  369. {
  370. char old_name[CLIENT_ID_LEN];
  371. Log( LOG_NOTICE|LOG_snotice, "Re-reading configuration NOW!" );
  372. NGIRCd_SignalRehash = false;
  373. /* Close down all listening sockets */
  374. Conn_ExitListeners( );
  375. /* Remember old server name */
  376. strlcpy( old_name, Conf_ServerName, sizeof old_name );
  377. /* Re-read configuration ... */
  378. Conf_Rehash( );
  379. /* Recover old server name: it can't be changed during run-time */
  380. if( strcmp( old_name, Conf_ServerName ) != 0 )
  381. {
  382. strlcpy( Conf_ServerName, old_name, sizeof Conf_ServerName );
  383. Log( LOG_ERR, "Can't change \"ServerName\" on runtime! Ignored new name." );
  384. }
  385. /* Create new pre-defined channels */
  386. Channel_InitPredefined( );
  387. /* Start listening on sockets */
  388. Conn_InitListeners( );
  389. /* Sync configuration with established connections */
  390. Conn_SyncServerStruct( );
  391. Log( LOG_NOTICE|LOG_snotice, "Re-reading of configuration done." );
  392. } /* NGIRCd_Rehash */
  393. /**
  394. * Initialize the signal handler.
  395. */
  396. static void
  397. Initialize_Signal_Handler( void )
  398. {
  399. /* Signal-Handler initialisieren: einige Signale
  400. * werden ignoriert, andere speziell behandelt. */
  401. #ifdef HAVE_SIGACTION
  402. /* sigaction() ist vorhanden */
  403. struct sigaction saction;
  404. /* Signal-Struktur initialisieren */
  405. memset( &saction, 0, sizeof( saction ));
  406. saction.sa_handler = Signal_Handler;
  407. #ifdef SA_RESTART
  408. saction.sa_flags |= SA_RESTART;
  409. #endif
  410. #ifdef SA_NOCLDWAIT
  411. saction.sa_flags |= SA_NOCLDWAIT;
  412. #endif
  413. /* Signal-Handler einhaengen */
  414. sigaction(SIGINT, &saction, NULL);
  415. sigaction(SIGQUIT, &saction, NULL);
  416. sigaction(SIGTERM, &saction, NULL);
  417. sigaction(SIGHUP, &saction, NULL);
  418. sigaction(SIGCHLD, &saction, NULL);
  419. /* einige Signale ignorieren */
  420. saction.sa_handler = SIG_IGN;
  421. sigaction(SIGPIPE, &saction, NULL);
  422. #else
  423. /* kein sigaction() vorhanden */
  424. /* Signal-Handler einhaengen */
  425. signal(SIGINT, Signal_Handler);
  426. signal(SIGQUIT, Signal_Handler);
  427. signal(SIGTERM, Signal_Handler);
  428. signal(SIGHUP, Signal_Handler);
  429. signal(SIGCHLD, Signal_Handler);
  430. /* einige Signale ignorieren */
  431. signal(SIGPIPE, SIG_IGN);
  432. #endif
  433. } /* Initialize_Signal_Handler */
  434. /**
  435. * Signal handler of ngIRCd.
  436. * This function is called whenever ngIRCd catches a signal sent by the
  437. * user and/or the system to it. For example SIGTERM and SIGHUP.
  438. * @param Signal Number of the signal to handle.
  439. */
  440. static void
  441. Signal_Handler( int Signal )
  442. {
  443. switch( Signal )
  444. {
  445. case SIGTERM:
  446. case SIGINT:
  447. case SIGQUIT:
  448. /* wir soll(t)en uns wohl beenden ... */
  449. NGIRCd_SignalQuit = true;
  450. break;
  451. case SIGHUP:
  452. /* Konfiguration neu einlesen: */
  453. NGIRCd_SignalRehash = true;
  454. break;
  455. case SIGCHLD:
  456. /* Child-Prozess wurde beendet. Zombies vermeiden: */
  457. while( waitpid( -1, NULL, WNOHANG ) > 0);
  458. break;
  459. #ifdef DEBUG
  460. default:
  461. /* unbekanntes bzw. unbehandeltes Signal */
  462. Log( LOG_DEBUG, "Got signal %d! Ignored.", Signal );
  463. #endif
  464. }
  465. } /* Signal_Handler */
  466. /**
  467. * Display copyright and version information of ngIRCd on the console.
  468. */
  469. static void
  470. Show_Version( void )
  471. {
  472. puts( NGIRCd_Version );
  473. puts( "Copyright (c)2001-2006 Alexander Barton (<alex@barton.de>) and Contributors." );
  474. puts( "Homepage: <http://ngircd.barton.de/>\n" );
  475. puts( "This is free software; see the source for copying conditions. There is NO" );
  476. puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
  477. } /* Show_Version */
  478. /**
  479. * Display a short help text on the console.
  480. * This help depends on the configuration of the executable and only shows
  481. * options that are actually enabled.
  482. */
  483. static void
  484. Show_Help( void )
  485. {
  486. #ifdef DEBUG
  487. puts( " -d, --debug log extra debug messages" );
  488. #endif
  489. puts( " -f, --config <f> use file <f> as configuration file" );
  490. puts( " -n, --nodaemon don't fork and don't detach from controlling terminal" );
  491. puts( " -p, --passive disable automatic connections to other servers" );
  492. #ifdef SNIFFER
  493. puts( " -s, --sniffer enable network sniffer and display all IRC traffic" );
  494. #endif
  495. puts( " -t, --configtest read, validate and display configuration; then exit" );
  496. puts( " --version output version information and exit" );
  497. puts( " --help display this help and exit" );
  498. } /* Show_Help */
  499. /**
  500. * Delete the file containing the process ID (PID).
  501. */
  502. static void
  503. Pidfile_Delete( void )
  504. {
  505. /* Pidfile configured? */
  506. if( ! Conf_PidFile[0] ) return;
  507. #ifdef DEBUG
  508. Log( LOG_DEBUG, "Removing PID file (%s) ...", Conf_PidFile );
  509. #endif
  510. if( unlink( Conf_PidFile ))
  511. Log( LOG_ERR, "Error unlinking PID file (%s): %s", Conf_PidFile, strerror( errno ));
  512. } /* Pidfile_Delete */
  513. /**
  514. * Create the file containing the process ID of ngIRCd ("PID file").
  515. * @param pid The process ID to be stored in this file.
  516. */
  517. static void
  518. Pidfile_Create(pid_t pid)
  519. {
  520. int pidfd;
  521. char pidbuf[64];
  522. int len;
  523. /* Pidfile configured? */
  524. if( ! Conf_PidFile[0] ) return;
  525. #ifdef DEBUG
  526. Log( LOG_DEBUG, "Creating PID file (%s) ...", Conf_PidFile );
  527. #endif
  528. pidfd = open( Conf_PidFile, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
  529. if ( pidfd < 0 ) {
  530. Log( LOG_ERR, "Error writing PID file (%s): %s", Conf_PidFile, strerror( errno ));
  531. return;
  532. }
  533. len = snprintf(pidbuf, sizeof pidbuf, "%ld\n", (long)pid);
  534. if (len < 0 || len >= (int)sizeof pidbuf) {
  535. Log( LOG_ERR, "Error converting pid");
  536. return;
  537. }
  538. if (write(pidfd, pidbuf, (size_t)len) != (ssize_t)len)
  539. Log( LOG_ERR, "Can't write PID file (%s): %s", Conf_PidFile, strerror( errno ));
  540. if( close(pidfd) != 0 )
  541. Log( LOG_ERR, "Error closing PID file (%s): %s", Conf_PidFile, strerror( errno ));
  542. } /* Pidfile_Create */
  543. /**
  544. * Redirect stdin, stdout and stderr to apropriate file handles.
  545. */
  546. static void
  547. Setup_FDStreams( void )
  548. {
  549. int fd;
  550. /* Test if we can open /dev/null for reading and writing. If not
  551. * we are most probably chrooted already and the server has been
  552. * restarted. So we simply don't try to redirect stdXXX ... */
  553. fd = open( "/dev/null", O_RDWR );
  554. if ( fd < 0 ) {
  555. Log(LOG_WARNING, "Could not open /dev/null: %s", strerror(errno));
  556. return;
  557. }
  558. fflush(stdout);
  559. fflush(stderr);
  560. /* Create new stdin(0), stdout(1) and stderr(2) descriptors */
  561. dup2( fd, 0 ); dup2( fd, 1 ); dup2( fd, 2 );
  562. /* Close newly opened file descriptor if not stdin/out/err */
  563. if( fd > 2 ) close( fd );
  564. } /* Setup_FDStreams */
  565. static bool
  566. NGIRCd_getNobodyID(uid_t *uid, gid_t *gid )
  567. {
  568. struct passwd *pwd;
  569. pwd = getpwnam("nobody");
  570. if (!pwd) return false;
  571. if ( !pwd->pw_uid || !pwd->pw_gid)
  572. return false;
  573. *uid = pwd->pw_uid;
  574. *gid = pwd->pw_gid;
  575. endpwent();
  576. return true;
  577. }
  578. static bool
  579. NGIRCd_Init( bool NGIRCd_NoDaemon )
  580. {
  581. static bool initialized;
  582. bool chrooted = false;
  583. struct passwd *pwd;
  584. struct group *grp;
  585. int real_errno;
  586. pid_t pid;
  587. if (initialized)
  588. return true;
  589. if( Conf_Chroot[0] ) {
  590. if( chdir( Conf_Chroot ) != 0 ) {
  591. Log( LOG_ERR, "Can't chdir() in ChrootDir (%s): %s", Conf_Chroot, strerror( errno ));
  592. return false;
  593. }
  594. if( chroot( Conf_Chroot ) != 0 ) {
  595. if (errno != EPERM) {
  596. Log( LOG_ERR, "Can't change root directory to \"%s\": %s",
  597. Conf_Chroot, strerror( errno ));
  598. return false;
  599. }
  600. } else {
  601. chrooted = true;
  602. Log( LOG_INFO, "Changed root and working directory to \"%s\".", Conf_Chroot );
  603. }
  604. }
  605. if (Conf_UID == 0) {
  606. Log(LOG_INFO, "ServerUID must not be 0, using \"nobody\" instead.", Conf_UID);
  607. if (! NGIRCd_getNobodyID(&Conf_UID, &Conf_GID)) {
  608. Log(LOG_WARNING, "Could not get user/group ID of user \"nobody\": %s",
  609. errno ? strerror(errno) : "not found" );
  610. return false;
  611. }
  612. }
  613. if (getgid() != Conf_GID) {
  614. /* Change group ID */
  615. if (setgid(Conf_GID) != 0) {
  616. real_errno = errno;
  617. Log( LOG_ERR, "Can't change group ID to %u: %s", Conf_GID, strerror( errno ));
  618. if (real_errno != EPERM)
  619. return false;
  620. }
  621. }
  622. if (getuid() != Conf_UID) {
  623. /* Change user ID */
  624. if (setuid(Conf_UID) != 0) {
  625. real_errno = errno;
  626. Log(LOG_ERR, "Can't change user ID to %u: %s", Conf_UID, strerror(errno));
  627. if (real_errno != EPERM)
  628. return false;
  629. }
  630. }
  631. initialized = true;
  632. /* Normally a child process is forked which isn't any longer
  633. * connected to ther controlling terminal. Use "--nodaemon"
  634. * to disable this "daemon mode" (useful for debugging). */
  635. if ( ! NGIRCd_NoDaemon ) {
  636. pid = fork( );
  637. if( pid > 0 ) {
  638. /* "Old" process: exit. */
  639. exit( 0 );
  640. }
  641. if( pid < 0 ) {
  642. /* Error!? */
  643. fprintf( stderr, "%s: Can't fork: %s!\nFatal error, exiting now ...\n",
  644. PACKAGE_NAME, strerror( errno ));
  645. exit( 1 );
  646. }
  647. /* New child process */
  648. (void)setsid( );
  649. chdir( "/" );
  650. /* Detach stdin, stdout and stderr */
  651. Setup_FDStreams( );
  652. }
  653. pid = getpid();
  654. Pidfile_Create( pid );
  655. /* Check UID/GID we are running as, can be different from values
  656. * configured (e. g. if we were already started with a UID>0. */
  657. Conf_UID = getuid();
  658. Conf_GID = getgid();
  659. pwd = getpwuid( Conf_UID );
  660. grp = getgrgid( Conf_GID );
  661. Log( LOG_INFO, "Running as user %s(%ld), group %s(%ld), with PID %ld.",
  662. pwd ? pwd->pw_name : "unknown", Conf_UID,
  663. grp ? grp->gr_name : "unknown", Conf_GID, pid);
  664. if ( chrooted ) {
  665. Log( LOG_INFO, "Running chrooted, chrootdir \"%s\".", Conf_Chroot );
  666. return true;
  667. } else {
  668. Log( LOG_INFO, "Not running chrooted." );
  669. }
  670. /* Change working directory to home directory of the user
  671. * we are running as (only when running in daemon mode and not in chroot) */
  672. if ( pwd ) {
  673. if (!NGIRCd_NoDaemon ) {
  674. if( chdir( pwd->pw_dir ) == 0 )
  675. Log( LOG_DEBUG, "Changed working directory to \"%s\" ...", pwd->pw_dir );
  676. else
  677. Log( LOG_ERR, "Can't change working directory to \"%s\": %s",
  678. pwd->pw_dir, strerror( errno ));
  679. }
  680. } else {
  681. Log( LOG_ERR, "Can't get user informaton for UID %d!?", Conf_UID );
  682. }
  683. return true;
  684. }
  685. /* -eof- */