conf.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605
  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. * Configuration management (reading, parsing & validation)
  12. */
  13. #include "portab.h"
  14. #include "imp.h"
  15. #include <assert.h>
  16. #include <errno.h>
  17. #ifdef PROTOTYPES
  18. # include <stdarg.h>
  19. #else
  20. # include <varargs.h>
  21. #endif
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <strings.h>
  26. #include <unistd.h>
  27. #include <pwd.h>
  28. #include <grp.h>
  29. #include <sys/types.h>
  30. #include <unistd.h>
  31. #ifdef HAVE_CTYPE_H
  32. # include <ctype.h>
  33. #endif
  34. #include "array.h"
  35. #include "ngircd.h"
  36. #include "conn.h"
  37. #include "channel.h"
  38. #include "defines.h"
  39. #include "log.h"
  40. #include "match.h"
  41. #include "tool.h"
  42. #include "exp.h"
  43. #include "conf.h"
  44. static bool Use_Log = true, Using_MotdFile = true;
  45. static CONF_SERVER New_Server;
  46. static int New_Server_Idx;
  47. static size_t Conf_Oper_Count;
  48. static size_t Conf_Channel_Count;
  49. static char Conf_MotdFile[FNAME_LEN];
  50. static void Set_Defaults PARAMS(( bool InitServers ));
  51. static bool Read_Config PARAMS(( bool ngircd_starting ));
  52. static bool Validate_Config PARAMS(( bool TestOnly, bool Rehash ));
  53. static void Handle_GLOBAL PARAMS(( int Line, char *Var, char *Arg ));
  54. static void Handle_OPERATOR PARAMS(( int Line, char *Var, char *Arg ));
  55. static void Handle_SERVER PARAMS(( int Line, char *Var, char *Arg ));
  56. static void Handle_CHANNEL PARAMS(( int Line, char *Var, char *Arg ));
  57. static void Config_Error PARAMS(( const int Level, const char *Format, ... ));
  58. static void Config_Error_NaN PARAMS(( const int LINE, const char *Value ));
  59. static void Config_Error_TooLong PARAMS(( const int LINE, const char *Value ));
  60. static void Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
  61. #ifdef WANT_IPV6
  62. #define DEFAULT_LISTEN_ADDRSTR "::,0.0.0.0"
  63. #else
  64. #define DEFAULT_LISTEN_ADDRSTR "0.0.0.0"
  65. #endif
  66. #ifdef SSL_SUPPORT
  67. struct SSLOptions Conf_SSLOptions;
  68. static void
  69. ConfSSL_Init(void)
  70. {
  71. free(Conf_SSLOptions.KeyFile);
  72. Conf_SSLOptions.KeyFile = NULL;
  73. free(Conf_SSLOptions.CertFile);
  74. Conf_SSLOptions.CertFile = NULL;
  75. free(Conf_SSLOptions.DHFile);
  76. Conf_SSLOptions.DHFile = NULL;
  77. array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
  78. }
  79. static bool
  80. ssl_print_configvar(const char *name, const char *file)
  81. {
  82. FILE *fp;
  83. if (!file) {
  84. printf(" %s =\n", name);
  85. return true;
  86. }
  87. fp = fopen(file, "r");
  88. if (fp)
  89. fclose(fp);
  90. else
  91. fprintf(stderr, "ERROR: %s \"%s\": %s\n",
  92. name, file, strerror(errno));
  93. printf(" %s = %s\n", name, file);
  94. return fp != NULL;
  95. }
  96. static bool
  97. ConfSSL_Puts(void)
  98. {
  99. bool ret;
  100. ret = ssl_print_configvar("SSLKeyFile", Conf_SSLOptions.KeyFile);
  101. if (!ssl_print_configvar("SSLCertFile", Conf_SSLOptions.CertFile))
  102. ret = false;
  103. if (!ssl_print_configvar("SSLDHFile", Conf_SSLOptions.DHFile))
  104. ret = false;
  105. if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
  106. puts(" SSLKeyFilePassword = <secret>");
  107. array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
  108. return ret;
  109. }
  110. #endif
  111. static char *
  112. strdup_warn(const char *str)
  113. {
  114. char *ptr = strdup(str);
  115. if (!ptr)
  116. Config_Error(LOG_ERR, "Could not allocate mem for string: %s", str);
  117. return ptr;
  118. }
  119. static void
  120. ports_puts(array *a)
  121. {
  122. size_t len;
  123. UINT16 *ports;
  124. len = array_length(a, sizeof(UINT16));
  125. if (len--) {
  126. ports = (UINT16*) array_start(a);
  127. printf("%u", (unsigned int) *ports);
  128. while (len--) {
  129. ports++;
  130. printf(", %u", (unsigned int) *ports);
  131. }
  132. }
  133. putc('\n', stdout);
  134. }
  135. static void
  136. ports_parse(array *a, int Line, char *Arg)
  137. {
  138. char *ptr;
  139. int port;
  140. UINT16 port16;
  141. array_trunc(a);
  142. /* Ports on that the server should listen. More port numbers
  143. * must be separated by "," */
  144. ptr = strtok( Arg, "," );
  145. while (ptr) {
  146. ngt_TrimStr(ptr);
  147. port = atoi(ptr);
  148. if (port > 0 && port < 0xFFFF) {
  149. port16 = (UINT16) port;
  150. if (!array_catb(a, (char*)&port16, sizeof port16))
  151. Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
  152. NGIRCd_ConfFile, Line, port, strerror(errno));
  153. } else {
  154. Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
  155. NGIRCd_ConfFile, Line, port );
  156. }
  157. ptr = strtok( NULL, "," );
  158. }
  159. }
  160. GLOBAL void
  161. Conf_Init( void )
  162. {
  163. Read_Config( true );
  164. Validate_Config(false, false);
  165. } /* Config_Init */
  166. GLOBAL bool
  167. Conf_Rehash( void )
  168. {
  169. if (!Read_Config(false))
  170. return false;
  171. Validate_Config(false, true);
  172. /* Update CLIENT structure of local server */
  173. Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
  174. return true;
  175. } /* Config_Rehash */
  176. static const char*
  177. yesno_to_str(int boolean_value)
  178. {
  179. if (boolean_value)
  180. return "yes";
  181. return "no";
  182. }
  183. static void
  184. opers_free(void)
  185. {
  186. struct Conf_Oper *op;
  187. size_t len;
  188. len = array_length(&Conf_Opers, sizeof(*op));
  189. op = array_start(&Conf_Opers);
  190. while (len--) {
  191. free(op->mask);
  192. op++;
  193. }
  194. array_free(&Conf_Opers);
  195. }
  196. static void
  197. opers_puts(void)
  198. {
  199. struct Conf_Oper *op;
  200. size_t len;
  201. len = array_length(&Conf_Opers, sizeof(*op));
  202. op = array_start(&Conf_Opers);
  203. while (len--) {
  204. assert(op->name[0]);
  205. puts("[OPERATOR]");
  206. printf(" Name = %s\n", op->name);
  207. printf(" Password = %s\n", op->pwd);
  208. printf(" Mask = %s\n\n", op->mask ? op->mask : "");
  209. op++;
  210. }
  211. }
  212. GLOBAL int
  213. Conf_Test( void )
  214. {
  215. /* Read configuration, validate and output it. */
  216. struct passwd *pwd;
  217. struct group *grp;
  218. unsigned int i;
  219. bool config_valid;
  220. size_t predef_channel_count;
  221. struct Conf_Channel *predef_chan;
  222. Use_Log = false;
  223. if (! Read_Config(true))
  224. return 1;
  225. config_valid = Validate_Config(true, false);
  226. /* If stdin and stdout ("you can read our nice message and we can
  227. * read in your keypress") are valid tty's, wait for a key: */
  228. if( isatty( fileno( stdin )) && isatty( fileno( stdout ))) {
  229. puts( "OK, press enter to see a dump of your service configuration ..." );
  230. getchar( );
  231. } else {
  232. puts( "Ok, dump of your server configuration follows:\n" );
  233. }
  234. puts( "[GLOBAL]" );
  235. printf(" Name = %s\n", Conf_ServerName);
  236. printf(" Info = %s\n", Conf_ServerInfo);
  237. #ifndef PAM
  238. printf(" Password = %s\n", Conf_ServerPwd);
  239. #endif
  240. printf(" WebircPassword = %s\n", Conf_WebircPwd);
  241. printf(" AdminInfo1 = %s\n", Conf_ServerAdmin1);
  242. printf(" AdminInfo2 = %s\n", Conf_ServerAdmin2);
  243. printf(" AdminEMail = %s\n", Conf_ServerAdminMail);
  244. if (Using_MotdFile) {
  245. printf(" MotdFile = %s\n", Conf_MotdFile);
  246. printf(" MotdPhrase =\n");
  247. } else {
  248. printf(" MotdFile = \n");
  249. printf(" MotdPhrase = %s\n", array_bytes(&Conf_Motd)
  250. ? (const char*) array_start(&Conf_Motd) : "");
  251. }
  252. printf(" ChrootDir = %s\n", Conf_Chroot);
  253. printf(" PidFile = %s\n", Conf_PidFile);
  254. printf(" Listen = %s\n", Conf_ListenAddress);
  255. fputs(" Ports = ", stdout);
  256. ports_puts(&Conf_ListenPorts);
  257. #ifdef SSL_SUPPORT
  258. fputs(" SSLPorts = ", stdout);
  259. ports_puts(&Conf_SSLOptions.ListenPorts);
  260. if (!ConfSSL_Puts())
  261. config_valid = false;
  262. #endif
  263. pwd = getpwuid(Conf_UID);
  264. if (pwd)
  265. printf(" ServerUID = %s\n", pwd->pw_name);
  266. else
  267. printf(" ServerUID = %ld\n", (long)Conf_UID);
  268. grp = getgrgid(Conf_GID);
  269. if (grp)
  270. printf(" ServerGID = %s\n", grp->gr_name);
  271. else
  272. printf(" ServerGID = %ld\n", (long)Conf_GID);
  273. #ifdef SYSLOG
  274. printf(" SyslogFacility = %s\n",
  275. ngt_SyslogFacilityName(Conf_SyslogFacility));
  276. #endif
  277. printf(" PingTimeout = %d\n", Conf_PingTimeout);
  278. printf(" PongTimeout = %d\n", Conf_PongTimeout);
  279. printf(" ConnectRetry = %d\n", Conf_ConnectRetry);
  280. printf(" OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
  281. printf(" OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
  282. printf(" AllowRemoteOper = %s\n", yesno_to_str(Conf_AllowRemoteOper));
  283. printf(" PredefChannelsOnly = %s\n", yesno_to_str(Conf_PredefChannelsOnly));
  284. printf(" NoDNS = %s\n", yesno_to_str(Conf_NoDNS));
  285. printf(" NoIdent = %s\n", yesno_to_str(Conf_NoIdent));
  286. printf(" NoPAM = %s\n", yesno_to_str(Conf_NoPAM));
  287. printf(" NoZeroConf = %s\n", yesno_to_str(Conf_NoZeroConf));
  288. #ifdef WANT_IPV6
  289. printf(" ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
  290. printf(" ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
  291. #endif
  292. printf(" MaxConnections = %ld\n", Conf_MaxConnections);
  293. printf(" MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
  294. printf(" MaxJoins = %d\n", Conf_MaxJoins > 0 ? Conf_MaxJoins : -1);
  295. printf(" MaxNickLength = %u\n\n", Conf_MaxNickLength - 1);
  296. opers_puts();
  297. for( i = 0; i < MAX_SERVERS; i++ ) {
  298. if( ! Conf_Server[i].name[0] ) continue;
  299. /* Valid "Server" section */
  300. puts( "[SERVER]" );
  301. printf( " Name = %s\n", Conf_Server[i].name );
  302. printf( " Host = %s\n", Conf_Server[i].host );
  303. printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
  304. #ifdef SSL_SUPPORT
  305. printf( " SSLConnect = %s\n", Conf_Server[i].SSLConnect?"yes":"no");
  306. #endif
  307. printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
  308. printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
  309. printf( " ServiceMask = %s\n", Conf_Server[i].svs_mask);
  310. printf( " Group = %d\n", Conf_Server[i].group );
  311. printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
  312. }
  313. predef_channel_count = array_length(&Conf_Channels, sizeof(*predef_chan));
  314. predef_chan = array_start(&Conf_Channels);
  315. for (i = 0; i < predef_channel_count; i++, predef_chan++) {
  316. if (!predef_chan->name[0])
  317. continue;
  318. /* Valid "Channel" section */
  319. puts( "[CHANNEL]" );
  320. printf(" Name = %s\n", predef_chan->name);
  321. printf(" Modes = %s\n", predef_chan->modes);
  322. printf(" Key = %s\n", predef_chan->key);
  323. printf(" MaxUsers = %lu\n", predef_chan->maxusers);
  324. printf(" Topic = %s\n", predef_chan->topic);
  325. printf(" KeyFile = %s\n\n", predef_chan->keyfile);
  326. }
  327. return (config_valid ? 0 : 1);
  328. } /* Conf_Test */
  329. GLOBAL void
  330. Conf_UnsetServer( CONN_ID Idx )
  331. {
  332. /* Set next time for next connection attempt, if this is a server
  333. * link that is (still) configured here. If the server is set as
  334. * "once", delete it from our configuration.
  335. * Non-Server-Connections will be silently ignored. */
  336. int i;
  337. time_t t;
  338. /* Check all our configured servers */
  339. for( i = 0; i < MAX_SERVERS; i++ ) {
  340. if( Conf_Server[i].conn_id != Idx ) continue;
  341. /* Gotcha! Mark server configuration as "unused": */
  342. Conf_Server[i].conn_id = NONE;
  343. if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
  344. /* Delete configuration here */
  345. Init_Server_Struct( &Conf_Server[i] );
  346. } else {
  347. /* Set time for next connect attempt */
  348. t = time(NULL);
  349. if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
  350. /* The connection has been "long", so we don't
  351. * require the next attempt to be delayed. */
  352. Conf_Server[i].lasttry =
  353. t - Conf_ConnectRetry + RECONNECT_DELAY;
  354. } else
  355. Conf_Server[i].lasttry = t;
  356. }
  357. }
  358. } /* Conf_UnsetServer */
  359. GLOBAL void
  360. Conf_SetServer( int ConfServer, CONN_ID Idx )
  361. {
  362. /* Set connection for specified configured server */
  363. assert( ConfServer > NONE );
  364. assert( Idx > NONE );
  365. Conf_Server[ConfServer].conn_id = Idx;
  366. } /* Conf_SetServer */
  367. GLOBAL int
  368. Conf_GetServer( CONN_ID Idx )
  369. {
  370. /* Get index of server in configuration structure */
  371. int i = 0;
  372. assert( Idx > NONE );
  373. for( i = 0; i < MAX_SERVERS; i++ ) {
  374. if( Conf_Server[i].conn_id == Idx ) return i;
  375. }
  376. return NONE;
  377. } /* Conf_GetServer */
  378. GLOBAL bool
  379. Conf_EnableServer( const char *Name, UINT16 Port )
  380. {
  381. /* Enable specified server and adjust port */
  382. int i;
  383. assert( Name != NULL );
  384. for( i = 0; i < MAX_SERVERS; i++ ) {
  385. if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
  386. /* Gotcha! Set port and enable server: */
  387. Conf_Server[i].port = Port;
  388. Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
  389. return (Conf_Server[i].port && Conf_Server[i].host[0]);
  390. }
  391. }
  392. return false;
  393. } /* Conf_EnableServer */
  394. GLOBAL bool
  395. Conf_EnablePassiveServer(const char *Name)
  396. {
  397. /* Enable specified server */
  398. int i;
  399. assert( Name != NULL );
  400. for (i = 0; i < MAX_SERVERS; i++) {
  401. if ((strcasecmp( Conf_Server[i].name, Name ) == 0) && (Conf_Server[i].port > 0)) {
  402. /* BINGO! Enable server */
  403. Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
  404. return true;
  405. }
  406. }
  407. return false;
  408. } /* Conf_EnablePassiveServer */
  409. GLOBAL bool
  410. Conf_DisableServer( const char *Name )
  411. {
  412. /* Enable specified server and adjust port */
  413. int i;
  414. assert( Name != NULL );
  415. for( i = 0; i < MAX_SERVERS; i++ ) {
  416. if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
  417. /* Gotcha! Disable and disconnect server: */
  418. Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
  419. if( Conf_Server[i].conn_id > NONE ) Conn_Close( Conf_Server[i].conn_id, NULL, "Server link terminated on operator request", true);
  420. return true;
  421. }
  422. }
  423. return false;
  424. } /* Conf_DisableServer */
  425. GLOBAL bool
  426. Conf_AddServer( const char *Name, UINT16 Port, const char *Host, const char *MyPwd, const char *PeerPwd )
  427. {
  428. /* Add new server to configuration */
  429. int i;
  430. assert( Name != NULL );
  431. assert( Host != NULL );
  432. assert( MyPwd != NULL );
  433. assert( PeerPwd != NULL );
  434. /* Search unused item in server configuration structure */
  435. for( i = 0; i < MAX_SERVERS; i++ ) {
  436. /* Is this item used? */
  437. if( ! Conf_Server[i].name[0] ) break;
  438. }
  439. if( i >= MAX_SERVERS ) return false;
  440. Init_Server_Struct( &Conf_Server[i] );
  441. strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
  442. strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
  443. strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
  444. strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
  445. Conf_Server[i].port = Port;
  446. Conf_Server[i].flags = CONF_SFLAG_ONCE;
  447. return true;
  448. } /* Conf_AddServer */
  449. /**
  450. * Check if the given nick name is an service
  451. */
  452. GLOBAL bool
  453. Conf_IsService(int ConfServer, const char *Nick)
  454. {
  455. return MatchCaseInsensitive(Conf_Server[ConfServer].svs_mask, Nick);
  456. } /* Conf_IsService */
  457. /**
  458. * Initialize configuration settings with their default values.
  459. */
  460. static void
  461. Set_Defaults(bool InitServers)
  462. {
  463. int i;
  464. strcpy(Conf_ServerName, "");
  465. snprintf(Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s",
  466. PACKAGE_NAME, PACKAGE_VERSION);
  467. strcpy(Conf_ServerPwd, "");
  468. strcpy(Conf_ServerAdmin1, "");
  469. strcpy(Conf_ServerAdmin2, "");
  470. strcpy(Conf_ServerAdminMail, "");
  471. strlcpy(Conf_MotdFile, SYSCONFDIR, sizeof(Conf_MotdFile));
  472. strlcat(Conf_MotdFile, MOTD_FILE, sizeof(Conf_MotdFile));
  473. Conf_UID = Conf_GID = 0;
  474. strlcpy(Conf_Chroot, CHROOT_DIR, sizeof(Conf_Chroot));
  475. strlcpy(Conf_PidFile, PID_FILE, sizeof(Conf_PidFile));
  476. free(Conf_ListenAddress);
  477. Conf_ListenAddress = NULL;
  478. Conf_PingTimeout = 120;
  479. Conf_PongTimeout = 20;
  480. Conf_ConnectRetry = 60;
  481. Conf_NoDNS = false;
  482. Conf_NoIdent = false;
  483. Conf_NoPAM = false;
  484. Conf_NoZeroConf = false;
  485. Conf_Oper_Count = 0;
  486. Conf_Channel_Count = 0;
  487. Conf_OperCanMode = false;
  488. Conf_OperServerMode = false;
  489. Conf_AllowRemoteOper = false;
  490. Conf_PredefChannelsOnly = false;
  491. Conf_ConnectIPv4 = true;
  492. Conf_ConnectIPv6 = true;
  493. Conf_MaxConnections = 0;
  494. Conf_MaxConnectionsIP = 5;
  495. Conf_MaxJoins = 10;
  496. Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
  497. #ifdef SYSLOG
  498. #ifdef LOG_LOCAL5
  499. Conf_SyslogFacility = LOG_LOCAL5;
  500. #else
  501. Conf_SyslogFacility = 0;
  502. #endif
  503. #endif
  504. /* Initialize server configuration structures */
  505. if (InitServers) {
  506. for (i = 0; i < MAX_SERVERS;
  507. Init_Server_Struct(&Conf_Server[i++]));
  508. }
  509. } /* Set_Defaults */
  510. static bool
  511. no_listenports(void)
  512. {
  513. size_t cnt = array_bytes(&Conf_ListenPorts);
  514. #ifdef SSL_SUPPORT
  515. cnt += array_bytes(&Conf_SSLOptions.ListenPorts);
  516. #endif
  517. return cnt == 0;
  518. }
  519. static void
  520. Read_Motd(const char *filename)
  521. {
  522. char line[127];
  523. FILE *fp;
  524. if (*filename == '\0')
  525. return;
  526. fp = fopen(filename, "r");
  527. if (!fp) {
  528. Log(LOG_WARNING, "Can't read MOTD file \"%s\": %s",
  529. filename, strerror(errno));
  530. return;
  531. }
  532. array_free(&Conf_Motd);
  533. Using_MotdFile = true;
  534. while (fgets(line, (int)sizeof line, fp)) {
  535. ngt_TrimLastChr( line, '\n');
  536. /* add text including \0 */
  537. if (!array_catb(&Conf_Motd, line, strlen(line) + 1)) {
  538. Log(LOG_WARNING, "Cannot add MOTD text: %s", strerror(errno));
  539. break;
  540. }
  541. }
  542. fclose(fp);
  543. }
  544. static bool
  545. Read_Config( bool ngircd_starting )
  546. {
  547. /* Read configuration file. */
  548. char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
  549. const UINT16 defaultport = 6667;
  550. int line, i, n;
  551. FILE *fd;
  552. /* Open configuration file */
  553. fd = fopen( NGIRCd_ConfFile, "r" );
  554. if( ! fd ) {
  555. /* No configuration file found! */
  556. Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
  557. NGIRCd_ConfFile, strerror( errno ));
  558. if (!ngircd_starting)
  559. return false;
  560. Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
  561. exit( 1 );
  562. }
  563. opers_free();
  564. Set_Defaults( ngircd_starting );
  565. Config_Error( LOG_INFO, "Reading configuration from \"%s\" ...", NGIRCd_ConfFile );
  566. /* Clean up server configuration structure: mark all already
  567. * configured servers as "once" so that they are deleted
  568. * after the next disconnect and delete all unused servers.
  569. * And delete all servers which are "duplicates" of servers
  570. * that are already marked as "once" (such servers have been
  571. * created by the last rehash but are now useless). */
  572. for( i = 0; i < MAX_SERVERS; i++ ) {
  573. if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
  574. else {
  575. /* This structure is in use ... */
  576. if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
  577. /* Check for duplicates */
  578. for( n = 0; n < MAX_SERVERS; n++ ) {
  579. if( n == i ) continue;
  580. if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
  581. Init_Server_Struct( &Conf_Server[n] );
  582. #ifdef DEBUG
  583. Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
  584. n, i );
  585. #endif
  586. }
  587. }
  588. } else {
  589. /* Mark server as "once" */
  590. Conf_Server[i].flags |= CONF_SFLAG_ONCE;
  591. Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
  592. }
  593. }
  594. }
  595. /* Initialize variables */
  596. line = 0;
  597. strcpy( section, "" );
  598. Init_Server_Struct( &New_Server );
  599. New_Server_Idx = NONE;
  600. #ifdef SSL_SUPPORT
  601. ConfSSL_Init();
  602. #endif
  603. /* Read configuration file */
  604. while( true ) {
  605. if( ! fgets( str, LINE_LEN, fd )) break;
  606. ngt_TrimStr( str );
  607. line++;
  608. /* Skip comments and empty lines */
  609. if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
  610. /* Is this the beginning of a new section? */
  611. if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
  612. strlcpy( section, str, sizeof( section ));
  613. if( strcasecmp( section, "[GLOBAL]" ) == 0 )
  614. continue;
  615. if( strcasecmp( section, "[SERVER]" ) == 0 ) {
  616. /* Check if there is already a server to add */
  617. if( New_Server.name[0] ) {
  618. /* Copy data to "real" server structure */
  619. assert( New_Server_Idx > NONE );
  620. Conf_Server[New_Server_Idx] = New_Server;
  621. }
  622. /* Re-init structure for new server */
  623. Init_Server_Struct( &New_Server );
  624. /* Search unused item in server configuration structure */
  625. for( i = 0; i < MAX_SERVERS; i++ ) {
  626. /* Is this item used? */
  627. if( ! Conf_Server[i].name[0] ) break;
  628. }
  629. if( i >= MAX_SERVERS ) {
  630. /* Oops, no free item found! */
  631. Config_Error( LOG_ERR, "Too many servers configured." );
  632. New_Server_Idx = NONE;
  633. }
  634. else New_Server_Idx = i;
  635. continue;
  636. }
  637. if (strcasecmp(section, "[CHANNEL]") == 0) {
  638. Conf_Channel_Count++;
  639. continue;
  640. }
  641. if (strcasecmp(section, "[OPERATOR]") == 0) {
  642. Conf_Oper_Count++;
  643. continue;
  644. }
  645. Config_Error( LOG_ERR, "%s, line %d: Unknown section \"%s\"!", NGIRCd_ConfFile, line, section );
  646. section[0] = 0x1;
  647. }
  648. if( section[0] == 0x1 ) continue;
  649. /* Split line into variable name and parameters */
  650. ptr = strchr( str, '=' );
  651. if( ! ptr ) {
  652. Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
  653. continue;
  654. }
  655. *ptr = '\0';
  656. var = str; ngt_TrimStr( var );
  657. arg = ptr + 1; ngt_TrimStr( arg );
  658. if( strcasecmp( section, "[GLOBAL]" ) == 0 ) Handle_GLOBAL( line, var, arg );
  659. else if( strcasecmp( section, "[OPERATOR]" ) == 0 ) Handle_OPERATOR( line, var, arg );
  660. else if( strcasecmp( section, "[SERVER]" ) == 0 ) Handle_SERVER( line, var, arg );
  661. else if( strcasecmp( section, "[CHANNEL]" ) == 0 ) Handle_CHANNEL( line, var, arg );
  662. else Config_Error( LOG_ERR, "%s, line %d: Variable \"%s\" outside section!", NGIRCd_ConfFile, line, var );
  663. }
  664. /* Close configuration file */
  665. fclose( fd );
  666. /* Check if there is still a server to add */
  667. if( New_Server.name[0] ) {
  668. /* Copy data to "real" server structure */
  669. assert( New_Server_Idx > NONE );
  670. Conf_Server[New_Server_Idx] = New_Server;
  671. }
  672. /* not a single listening port? Add default. */
  673. if (no_listenports() &&
  674. !array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport))
  675. {
  676. Config_Error(LOG_ALERT, "Could not add default listening Port %u: %s",
  677. (unsigned int) defaultport, strerror(errno));
  678. exit(1);
  679. }
  680. if (!Conf_ListenAddress)
  681. Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
  682. if (!Conf_ListenAddress) {
  683. Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
  684. exit(1);
  685. }
  686. /* No MOTD phrase configured? (re)try motd file. */
  687. if (array_bytes(&Conf_Motd) == 0)
  688. Read_Motd(Conf_MotdFile);
  689. return true;
  690. } /* Read_Config */
  691. static bool
  692. Check_ArgIsTrue( const char *Arg )
  693. {
  694. if( strcasecmp( Arg, "yes" ) == 0 ) return true;
  695. if( strcasecmp( Arg, "true" ) == 0 ) return true;
  696. if( atoi( Arg ) != 0 ) return true;
  697. return false;
  698. } /* Check_ArgIsTrue */
  699. static unsigned int
  700. Handle_MaxNickLength(int Line, const char *Arg)
  701. {
  702. unsigned new;
  703. new = (unsigned) atoi(Arg) + 1;
  704. if (new > CLIENT_NICK_LEN) {
  705. Config_Error(LOG_WARNING,
  706. "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
  707. NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
  708. return CLIENT_NICK_LEN;
  709. }
  710. if (new < 2) {
  711. Config_Error(LOG_WARNING,
  712. "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
  713. NGIRCd_ConfFile, Line);
  714. return 2;
  715. }
  716. return new;
  717. } /* Handle_MaxNickLength */
  718. static void
  719. Handle_GLOBAL( int Line, char *Var, char *Arg )
  720. {
  721. struct passwd *pwd;
  722. struct group *grp;
  723. size_t len;
  724. assert( Line > 0 );
  725. assert( Var != NULL );
  726. assert( Arg != NULL );
  727. if( strcasecmp( Var, "Name" ) == 0 ) {
  728. /* Server name */
  729. len = strlcpy( Conf_ServerName, Arg, sizeof( Conf_ServerName ));
  730. if (len >= sizeof( Conf_ServerName ))
  731. Config_Error_TooLong( Line, Var );
  732. return;
  733. }
  734. if( strcasecmp( Var, "Info" ) == 0 ) {
  735. /* Info text of server */
  736. len = strlcpy( Conf_ServerInfo, Arg, sizeof( Conf_ServerInfo ));
  737. if (len >= sizeof( Conf_ServerInfo ))
  738. Config_Error_TooLong ( Line, Var );
  739. return;
  740. }
  741. if( strcasecmp( Var, "Password" ) == 0 ) {
  742. /* Global server password */
  743. len = strlcpy( Conf_ServerPwd, Arg, sizeof( Conf_ServerPwd ));
  744. if (len >= sizeof( Conf_ServerPwd ))
  745. Config_Error_TooLong( Line, Var );
  746. return;
  747. }
  748. if (strcasecmp(Var, "WebircPassword") == 0) {
  749. /* Password required for WEBIRC command */
  750. len = strlcpy(Conf_WebircPwd, Arg, sizeof(Conf_WebircPwd));
  751. if (len >= sizeof(Conf_WebircPwd))
  752. Config_Error_TooLong(Line, Var);
  753. return;
  754. }
  755. if( strcasecmp( Var, "AdminInfo1" ) == 0 ) {
  756. /* Administrative info #1 */
  757. len = strlcpy( Conf_ServerAdmin1, Arg, sizeof( Conf_ServerAdmin1 ));
  758. if (len >= sizeof( Conf_ServerAdmin1 ))
  759. Config_Error_TooLong ( Line, Var );
  760. return;
  761. }
  762. if( strcasecmp( Var, "AdminInfo2" ) == 0 ) {
  763. /* Administrative info #2 */
  764. len = strlcpy( Conf_ServerAdmin2, Arg, sizeof( Conf_ServerAdmin2 ));
  765. if (len >= sizeof( Conf_ServerAdmin2 ))
  766. Config_Error_TooLong ( Line, Var );
  767. return;
  768. }
  769. if( strcasecmp( Var, "AdminEMail" ) == 0 ) {
  770. /* Administrative email contact */
  771. len = strlcpy( Conf_ServerAdminMail, Arg, sizeof( Conf_ServerAdminMail ));
  772. if (len >= sizeof( Conf_ServerAdminMail ))
  773. Config_Error_TooLong( Line, Var );
  774. return;
  775. }
  776. if( strcasecmp( Var, "Ports" ) == 0 ) {
  777. ports_parse(&Conf_ListenPorts, Line, Arg);
  778. return;
  779. }
  780. if( strcasecmp( Var, "MotdFile" ) == 0 ) {
  781. len = strlcpy( Conf_MotdFile, Arg, sizeof( Conf_MotdFile ));
  782. if (len >= sizeof( Conf_MotdFile ))
  783. Config_Error_TooLong( Line, Var );
  784. Read_Motd(Arg);
  785. return;
  786. }
  787. if( strcasecmp( Var, "MotdPhrase" ) == 0 ) {
  788. /* "Message of the day" phrase (instead of file) */
  789. len = strlen(Arg);
  790. if (len == 0)
  791. return;
  792. if (len >= LINE_LEN) {
  793. Config_Error_TooLong( Line, Var );
  794. return;
  795. }
  796. if (!array_copyb(&Conf_Motd, Arg, len + 1))
  797. Config_Error(LOG_WARNING, "%s, line %d: Could not append MotdPhrase: %s",
  798. NGIRCd_ConfFile, Line, strerror(errno));
  799. Using_MotdFile = false;
  800. return;
  801. }
  802. if( strcasecmp( Var, "ChrootDir" ) == 0 ) {
  803. /* directory for chroot() */
  804. len = strlcpy( Conf_Chroot, Arg, sizeof( Conf_Chroot ));
  805. if (len >= sizeof( Conf_Chroot ))
  806. Config_Error_TooLong( Line, Var );
  807. return;
  808. }
  809. if ( strcasecmp( Var, "PidFile" ) == 0 ) {
  810. /* name of pidfile */
  811. len = strlcpy( Conf_PidFile, Arg, sizeof( Conf_PidFile ));
  812. if (len >= sizeof( Conf_PidFile ))
  813. Config_Error_TooLong( Line, Var );
  814. return;
  815. }
  816. if( strcasecmp( Var, "ServerUID" ) == 0 ) {
  817. /* UID the daemon should switch to */
  818. pwd = getpwnam( Arg );
  819. if( pwd ) Conf_UID = pwd->pw_uid;
  820. else {
  821. #ifdef HAVE_ISDIGIT
  822. if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
  823. else
  824. #endif
  825. Conf_UID = (unsigned int)atoi( Arg );
  826. }
  827. return;
  828. }
  829. if( strcasecmp( Var, "ServerGID" ) == 0 ) {
  830. /* GID the daemon should use */
  831. grp = getgrnam( Arg );
  832. if( grp ) Conf_GID = grp->gr_gid;
  833. else {
  834. #ifdef HAVE_ISDIGIT
  835. if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
  836. else
  837. #endif
  838. Conf_GID = (unsigned int)atoi( Arg );
  839. }
  840. return;
  841. }
  842. if( strcasecmp( Var, "PingTimeout" ) == 0 ) {
  843. /* PING timeout */
  844. Conf_PingTimeout = atoi( Arg );
  845. if( Conf_PingTimeout < 5 ) {
  846. Config_Error( LOG_WARNING, "%s, line %d: Value of \"PingTimeout\" too low!",
  847. NGIRCd_ConfFile, Line );
  848. Conf_PingTimeout = 5;
  849. }
  850. return;
  851. }
  852. if( strcasecmp( Var, "PongTimeout" ) == 0 ) {
  853. /* PONG timeout */
  854. Conf_PongTimeout = atoi( Arg );
  855. if( Conf_PongTimeout < 5 ) {
  856. Config_Error( LOG_WARNING, "%s, line %d: Value of \"PongTimeout\" too low!",
  857. NGIRCd_ConfFile, Line );
  858. Conf_PongTimeout = 5;
  859. }
  860. return;
  861. }
  862. if( strcasecmp( Var, "ConnectRetry" ) == 0 ) {
  863. /* Seconds between connection attempts to other servers */
  864. Conf_ConnectRetry = atoi( Arg );
  865. if( Conf_ConnectRetry < 5 ) {
  866. Config_Error( LOG_WARNING, "%s, line %d: Value of \"ConnectRetry\" too low!",
  867. NGIRCd_ConfFile, Line );
  868. Conf_ConnectRetry = 5;
  869. }
  870. return;
  871. }
  872. if( strcasecmp( Var, "PredefChannelsOnly" ) == 0 ) {
  873. /* Should we only allow pre-defined-channels? (i.e. users cannot create their own channels) */
  874. Conf_PredefChannelsOnly = Check_ArgIsTrue( Arg );
  875. return;
  876. }
  877. if( strcasecmp( Var, "NoDNS" ) == 0 ) {
  878. /* don't do reverse dns lookups when clients connect? */
  879. Conf_NoDNS = Check_ArgIsTrue( Arg );
  880. return;
  881. }
  882. if (strcasecmp(Var, "NoIdent") == 0) {
  883. /* don't do IDENT lookups when clients connect? */
  884. Conf_NoIdent = Check_ArgIsTrue(Arg);
  885. #ifndef IDENTAUTH
  886. if (!Conf_NoIdent) {
  887. /* user has enabled ident lookups explicitly, but ... */
  888. Config_Error(LOG_WARNING,
  889. "%s: line %d: NoIdent=False, but ngircd was built without IDENT support",
  890. NGIRCd_ConfFile, Line);
  891. }
  892. #endif
  893. return;
  894. }
  895. if(strcasecmp(Var, "NoPAM") == 0) {
  896. /* don't use PAM library to authenticate users */
  897. Conf_NoPAM = Check_ArgIsTrue(Arg);
  898. return;
  899. }
  900. if(strcasecmp(Var, "NoZeroConf") == 0) {
  901. /* don't register services using ZeroConf */
  902. Conf_NoZeroConf = Check_ArgIsTrue(Arg);
  903. return;
  904. }
  905. #ifdef WANT_IPV6
  906. /* the default setting for all the WANT_IPV6 special options is 'true' */
  907. if( strcasecmp( Var, "ConnectIPv6" ) == 0 ) {
  908. /* connect to other hosts using ipv6, if they have an AAAA record? */
  909. Conf_ConnectIPv6 = Check_ArgIsTrue( Arg );
  910. return;
  911. }
  912. if( strcasecmp( Var, "ConnectIPv4" ) == 0 ) {
  913. /* connect to other hosts using ipv4.
  914. * again, this can be used for ipv6-only setups */
  915. Conf_ConnectIPv4 = Check_ArgIsTrue( Arg );
  916. return;
  917. }
  918. #endif
  919. if( strcasecmp( Var, "OperCanUseMode" ) == 0 ) {
  920. /* Are IRC operators allowed to use MODE in channels they aren't Op in? */
  921. Conf_OperCanMode = Check_ArgIsTrue( Arg );
  922. return;
  923. }
  924. if( strcasecmp( Var, "OperServerMode" ) == 0 ) {
  925. /* Mask IRC operator as if coming from the server? (ircd-irc2 compat hack) */
  926. Conf_OperServerMode = Check_ArgIsTrue( Arg );
  927. return;
  928. }
  929. if(strcasecmp(Var, "AllowRemoteOper") == 0) {
  930. /* Are remote IRC operators allowed to control this server? */
  931. Conf_AllowRemoteOper = Check_ArgIsTrue(Arg);
  932. return;
  933. }
  934. if( strcasecmp( Var, "MaxConnections" ) == 0 ) {
  935. /* Maximum number of connections. 0 -> "no limit". */
  936. #ifdef HAVE_ISDIGIT
  937. if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var);
  938. else
  939. #endif
  940. Conf_MaxConnections = atol( Arg );
  941. return;
  942. }
  943. if( strcasecmp( Var, "MaxConnectionsIP" ) == 0 ) {
  944. /* Maximum number of simultaneous connections from one IP. 0 -> "no limit" */
  945. #ifdef HAVE_ISDIGIT
  946. if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
  947. else
  948. #endif
  949. Conf_MaxConnectionsIP = atoi( Arg );
  950. return;
  951. }
  952. if( strcasecmp( Var, "MaxJoins" ) == 0 ) {
  953. /* Maximum number of channels a user can join. 0 -> "no limit". */
  954. #ifdef HAVE_ISDIGIT
  955. if( ! isdigit( (int)*Arg )) Config_Error_NaN( Line, Var );
  956. else
  957. #endif
  958. Conf_MaxJoins = atoi( Arg );
  959. return;
  960. }
  961. if( strcasecmp( Var, "MaxNickLength" ) == 0 ) {
  962. /* Maximum length of a nick name; must be same on all servers
  963. * within the IRC network! */
  964. Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
  965. return;
  966. }
  967. if( strcasecmp( Var, "Listen" ) == 0 ) {
  968. /* IP-Address to bind sockets */
  969. if (Conf_ListenAddress) {
  970. Config_Error(LOG_ERR, "Multiple Listen= options, ignoring: %s", Arg);
  971. return;
  972. }
  973. Conf_ListenAddress = strdup_warn(Arg);
  974. /*
  975. * if allocation fails, we're in trouble:
  976. * we cannot ignore the error -- otherwise ngircd
  977. * would listen on all interfaces.
  978. */
  979. if (!Conf_ListenAddress) {
  980. Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
  981. exit(1);
  982. }
  983. return;
  984. }
  985. #ifdef SSL_SUPPORT
  986. if( strcasecmp( Var, "SSLPorts" ) == 0 ) {
  987. ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
  988. return;
  989. }
  990. if( strcasecmp( Var, "SSLKeyFile" ) == 0 ) {
  991. assert(Conf_SSLOptions.KeyFile == NULL );
  992. Conf_SSLOptions.KeyFile = strdup_warn(Arg);
  993. return;
  994. }
  995. if( strcasecmp( Var, "SSLCertFile" ) == 0 ) {
  996. assert(Conf_SSLOptions.CertFile == NULL );
  997. Conf_SSLOptions.CertFile = strdup_warn(Arg);
  998. return;
  999. }
  1000. if( strcasecmp( Var, "SSLKeyFilePassword" ) == 0 ) {
  1001. assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
  1002. if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
  1003. Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Could not copy %s: %s!",
  1004. NGIRCd_ConfFile, Line, Var, strerror(errno));
  1005. return;
  1006. }
  1007. if( strcasecmp( Var, "SSLDHFile" ) == 0 ) {
  1008. assert(Conf_SSLOptions.DHFile == NULL);
  1009. Conf_SSLOptions.DHFile = strdup_warn( Arg );
  1010. return;
  1011. }
  1012. #endif
  1013. #ifdef SYSLOG
  1014. if (strcasecmp(Var, "SyslogFacility") == 0) {
  1015. Conf_SyslogFacility = ngt_SyslogFacilityID(Arg,
  1016. Conf_SyslogFacility);
  1017. return;
  1018. }
  1019. #endif
  1020. Config_Error(LOG_ERR, "%s, line %d (section \"Global\"): Unknown variable \"%s\"!",
  1021. NGIRCd_ConfFile, Line, Var);
  1022. } /* Handle_GLOBAL */
  1023. static void
  1024. Handle_OPERATOR( int Line, char *Var, char *Arg )
  1025. {
  1026. size_t len;
  1027. struct Conf_Oper *op;
  1028. assert( Line > 0 );
  1029. assert( Var != NULL );
  1030. assert( Arg != NULL );
  1031. assert( Conf_Oper_Count > 0 );
  1032. op = array_alloc(&Conf_Opers, sizeof(*op), Conf_Oper_Count - 1);
  1033. if (!op) {
  1034. Config_Error(LOG_ERR, "Could not allocate memory for operator (%d:%s = %s)", Line, Var, Arg);
  1035. return;
  1036. }
  1037. if (strcasecmp(Var, "Name") == 0) {
  1038. /* Name of IRC operator */
  1039. len = strlcpy(op->name, Arg, sizeof(op->name));
  1040. if (len >= sizeof(op->name))
  1041. Config_Error_TooLong(Line, Var);
  1042. return;
  1043. }
  1044. if (strcasecmp(Var, "Password") == 0) {
  1045. /* Password of IRC operator */
  1046. len = strlcpy(op->pwd, Arg, sizeof(op->pwd));
  1047. if (len >= sizeof(op->pwd))
  1048. Config_Error_TooLong(Line, Var);
  1049. return;
  1050. }
  1051. if (strcasecmp(Var, "Mask") == 0) {
  1052. if (op->mask)
  1053. return; /* Hostname already configured */
  1054. op->mask = strdup_warn( Arg );
  1055. return;
  1056. }
  1057. Config_Error( LOG_ERR, "%s, line %d (section \"Operator\"): Unknown variable \"%s\"!",
  1058. NGIRCd_ConfFile, Line, Var );
  1059. } /* Handle_OPERATOR */
  1060. static void
  1061. Handle_SERVER( int Line, char *Var, char *Arg )
  1062. {
  1063. long port;
  1064. size_t len;
  1065. assert( Line > 0 );
  1066. assert( Var != NULL );
  1067. assert( Arg != NULL );
  1068. /* Ignore server block if no space is left in server configuration structure */
  1069. if( New_Server_Idx <= NONE ) return;
  1070. if( strcasecmp( Var, "Host" ) == 0 ) {
  1071. /* Hostname of the server */
  1072. len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
  1073. if (len >= sizeof( New_Server.host ))
  1074. Config_Error_TooLong ( Line, Var );
  1075. return;
  1076. }
  1077. if( strcasecmp( Var, "Name" ) == 0 ) {
  1078. /* Name of the server ("Nick"/"ID") */
  1079. len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
  1080. if (len >= sizeof( New_Server.name ))
  1081. Config_Error_TooLong( Line, Var );
  1082. return;
  1083. }
  1084. if (strcasecmp(Var, "Bind") == 0) {
  1085. if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
  1086. return;
  1087. Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
  1088. NGIRCd_ConfFile, Line, Arg);
  1089. return;
  1090. }
  1091. if( strcasecmp( Var, "MyPassword" ) == 0 ) {
  1092. /* Password of this server which is sent to the peer */
  1093. if (*Arg == ':') {
  1094. Config_Error(LOG_ERR,
  1095. "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
  1096. NGIRCd_ConfFile, Line);
  1097. }
  1098. len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
  1099. if (len >= sizeof( New_Server.pwd_in ))
  1100. Config_Error_TooLong( Line, Var );
  1101. return;
  1102. }
  1103. if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
  1104. /* Passwort of the peer which must be received */
  1105. len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
  1106. if (len >= sizeof( New_Server.pwd_out ))
  1107. Config_Error_TooLong( Line, Var );
  1108. return;
  1109. }
  1110. if( strcasecmp( Var, "Port" ) == 0 ) {
  1111. /* Port to which this server should connect */
  1112. port = atol( Arg );
  1113. if( port > 0 && port < 0xFFFF )
  1114. New_Server.port = (UINT16)port;
  1115. else
  1116. Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Illegal port number %ld!",
  1117. NGIRCd_ConfFile, Line, port );
  1118. return;
  1119. }
  1120. #ifdef SSL_SUPPORT
  1121. if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
  1122. New_Server.SSLConnect = Check_ArgIsTrue(Arg);
  1123. return;
  1124. }
  1125. #endif
  1126. if( strcasecmp( Var, "Group" ) == 0 ) {
  1127. /* Server group */
  1128. #ifdef HAVE_ISDIGIT
  1129. if( ! isdigit( (int)*Arg ))
  1130. Config_Error_NaN( Line, Var );
  1131. else
  1132. #endif
  1133. New_Server.group = atoi( Arg );
  1134. return;
  1135. }
  1136. if( strcasecmp( Var, "Passive" ) == 0 ) {
  1137. if (Check_ArgIsTrue(Arg))
  1138. New_Server.flags |= CONF_SFLAG_DISABLED;
  1139. return;
  1140. }
  1141. if (strcasecmp(Var, "ServiceMask") == 0) {
  1142. len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
  1143. sizeof(New_Server.svs_mask));
  1144. if (len >= sizeof(New_Server.svs_mask))
  1145. Config_Error_TooLong(Line, Var);
  1146. return;
  1147. }
  1148. Config_Error( LOG_ERR, "%s, line %d (section \"Server\"): Unknown variable \"%s\"!",
  1149. NGIRCd_ConfFile, Line, Var );
  1150. } /* Handle_SERVER */
  1151. static bool
  1152. Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
  1153. {
  1154. size_t size = sizeof(new_chan->name);
  1155. char *dest = new_chan->name;
  1156. if (!Channel_IsValidName(name)) {
  1157. /*
  1158. * maybe user forgot to add a '#'.
  1159. * This is only here for user convenience.
  1160. */
  1161. *dest = '#';
  1162. --size;
  1163. ++dest;
  1164. }
  1165. return size > strlcpy(dest, name, size);
  1166. }
  1167. static void
  1168. Handle_CHANNEL(int Line, char *Var, char *Arg)
  1169. {
  1170. size_t len;
  1171. size_t chancount;
  1172. struct Conf_Channel *chan;
  1173. assert( Line > 0 );
  1174. assert( Var != NULL );
  1175. assert( Arg != NULL );
  1176. assert(Conf_Channel_Count > 0);
  1177. chancount = Conf_Channel_Count - 1;
  1178. chan = array_alloc(&Conf_Channels, sizeof(*chan), chancount);
  1179. if (!chan) {
  1180. Config_Error(LOG_ERR, "Could not allocate memory for predefined channel (%d:%s = %s)", Line, Var, Arg);
  1181. return;
  1182. }
  1183. if (strcasecmp(Var, "Name") == 0) {
  1184. if (!Handle_Channelname(chan, Arg))
  1185. Config_Error_TooLong(Line, Var);
  1186. return;
  1187. }
  1188. if (strcasecmp(Var, "Modes") == 0) {
  1189. /* Initial modes */
  1190. len = strlcpy(chan->modes, Arg, sizeof(chan->modes));
  1191. if (len >= sizeof(chan->modes))
  1192. Config_Error_TooLong( Line, Var );
  1193. return;
  1194. }
  1195. if( strcasecmp( Var, "Topic" ) == 0 ) {
  1196. /* Initial topic */
  1197. len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
  1198. if (len >= sizeof(chan->topic))
  1199. Config_Error_TooLong( Line, Var );
  1200. return;
  1201. }
  1202. if( strcasecmp( Var, "Key" ) == 0 ) {
  1203. /* Initial Channel Key (mode k) */
  1204. len = strlcpy(chan->key, Arg, sizeof(chan->key));
  1205. if (len >= sizeof(chan->key))
  1206. Config_Error_TooLong(Line, Var);
  1207. return;
  1208. }
  1209. if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
  1210. /* maximum user limit, mode l */
  1211. chan->maxusers = (unsigned long) atol(Arg);
  1212. if (chan->maxusers == 0)
  1213. Config_Error_NaN(Line, Var);
  1214. return;
  1215. }
  1216. if (strcasecmp(Var, "KeyFile") == 0) {
  1217. /* channel keys */
  1218. len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
  1219. if (len >= sizeof(chan->keyfile))
  1220. Config_Error_TooLong(Line, Var);
  1221. return;
  1222. }
  1223. Config_Error( LOG_ERR, "%s, line %d (section \"Channel\"): Unknown variable \"%s\"!",
  1224. NGIRCd_ConfFile, Line, Var );
  1225. } /* Handle_CHANNEL */
  1226. static bool
  1227. Validate_Config(bool Configtest, bool Rehash)
  1228. {
  1229. /* Validate configuration settings. */
  1230. #ifdef DEBUG
  1231. int i, servers, servers_once;
  1232. #endif
  1233. bool config_valid = true;
  1234. char *ptr;
  1235. /* Validate configured server name, see RFC 2812 section 2.3.1 */
  1236. ptr = Conf_ServerName;
  1237. do {
  1238. if (*ptr >= 'a' && *ptr <= 'z') continue;
  1239. if (*ptr >= 'A' && *ptr <= 'Z') continue;
  1240. if (*ptr >= '0' && *ptr <= '9') continue;
  1241. if (ptr > Conf_ServerName) {
  1242. if (*ptr == '.' || *ptr == '-')
  1243. continue;
  1244. }
  1245. Conf_ServerName[0] = '\0';
  1246. break;
  1247. } while (*(++ptr));
  1248. if (!Conf_ServerName[0]) {
  1249. /* No server name configured! */
  1250. config_valid = false;
  1251. Config_Error(LOG_ALERT,
  1252. "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
  1253. NGIRCd_ConfFile);
  1254. if (!Configtest && !Rehash) {
  1255. Config_Error(LOG_ALERT,
  1256. "%s exiting due to fatal errors!",
  1257. PACKAGE_NAME);
  1258. exit(1);
  1259. }
  1260. }
  1261. if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
  1262. /* No dot in server name! */
  1263. config_valid = false;
  1264. Config_Error(LOG_ALERT,
  1265. "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
  1266. NGIRCd_ConfFile);
  1267. if (!Configtest) {
  1268. Config_Error(LOG_ALERT,
  1269. "%s exiting due to fatal errors!",
  1270. PACKAGE_NAME);
  1271. exit(1);
  1272. }
  1273. }
  1274. #ifdef STRICT_RFC
  1275. if (!Conf_ServerAdminMail[0]) {
  1276. /* No administrative contact configured! */
  1277. config_valid = false;
  1278. Config_Error(LOG_ALERT,
  1279. "No administrator email address configured in \"%s\" ('AdminEMail')!",
  1280. NGIRCd_ConfFile);
  1281. if (!Configtest) {
  1282. Config_Error(LOG_ALERT,
  1283. "%s exiting due to fatal errors!",
  1284. PACKAGE_NAME);
  1285. exit(1);
  1286. }
  1287. }
  1288. #endif
  1289. if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
  1290. && !Conf_ServerAdminMail[0]) {
  1291. /* No administrative information configured! */
  1292. Config_Error(LOG_WARNING,
  1293. "No administrative information configured but required by RFC!");
  1294. }
  1295. #ifdef PAM
  1296. if (Conf_ServerPwd[0])
  1297. Config_Error(LOG_ERR,
  1298. "This server uses PAM, \"Password\" will be ignored!");
  1299. #endif
  1300. #ifdef DEBUG
  1301. servers = servers_once = 0;
  1302. for (i = 0; i < MAX_SERVERS; i++) {
  1303. if (Conf_Server[i].name[0]) {
  1304. servers++;
  1305. if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
  1306. servers_once++;
  1307. }
  1308. }
  1309. Log(LOG_DEBUG,
  1310. "Configuration: Operators=%d, Servers=%d[%d], Channels=%d",
  1311. Conf_Oper_Count, servers, servers_once, Conf_Channel_Count);
  1312. #endif
  1313. return config_valid;
  1314. } /* Validate_Config */
  1315. static void
  1316. Config_Error_TooLong ( const int Line, const char *Item )
  1317. {
  1318. Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
  1319. }
  1320. static void
  1321. Config_Error_NaN( const int Line, const char *Item )
  1322. {
  1323. Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
  1324. NGIRCd_ConfFile, Line, Item );
  1325. }
  1326. #ifdef PROTOTYPES
  1327. static void Config_Error( const int Level, const char *Format, ... )
  1328. #else
  1329. static void Config_Error( Level, Format, va_alist )
  1330. const int Level;
  1331. const char *Format;
  1332. va_dcl
  1333. #endif
  1334. {
  1335. /* Error! Write to console and/or logfile. */
  1336. char msg[MAX_LOG_MSG_LEN];
  1337. va_list ap;
  1338. assert( Format != NULL );
  1339. #ifdef PROTOTYPES
  1340. va_start( ap, Format );
  1341. #else
  1342. va_start( ap );
  1343. #endif
  1344. vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
  1345. va_end( ap );
  1346. /* During "normal operations" the log functions of the daemon should
  1347. * be used, but during testing of the configuration file, all messages
  1348. * should go directly to the console: */
  1349. if (Use_Log) Log( Level, "%s", msg );
  1350. else puts( msg );
  1351. } /* Config_Error */
  1352. #ifdef DEBUG
  1353. GLOBAL void
  1354. Conf_DebugDump(void)
  1355. {
  1356. int i;
  1357. Log(LOG_DEBUG, "Configured servers:");
  1358. for (i = 0; i < MAX_SERVERS; i++) {
  1359. if (! Conf_Server[i].name[0])
  1360. continue;
  1361. Log(LOG_DEBUG,
  1362. " - %s: %s:%d, last=%ld, group=%d, flags=%d, conn=%d",
  1363. Conf_Server[i].name, Conf_Server[i].host,
  1364. Conf_Server[i].port, Conf_Server[i].lasttry,
  1365. Conf_Server[i].group, Conf_Server[i].flags,
  1366. Conf_Server[i].conn_id);
  1367. }
  1368. } /* Conf_DebugDump */
  1369. #endif
  1370. static void
  1371. Init_Server_Struct( CONF_SERVER *Server )
  1372. {
  1373. /* Initialize server configuration structur to default values */
  1374. assert( Server != NULL );
  1375. memset( Server, 0, sizeof (CONF_SERVER) );
  1376. Server->group = NONE;
  1377. Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
  1378. if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
  1379. Proc_InitStruct(&Server->res_stat);
  1380. Server->conn_id = NONE;
  1381. memset(&Server->bind_addr, 0, sizeof(&Server->bind_addr));
  1382. } /* Init_Server_Struct */
  1383. /* -eof- */