ngircd.c 20 KB

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