conf.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2012 Alexander Barton (alex@barton.de) and Contributors.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * Please read the file COPYING, README and AUTHORS for more information.
  10. */
  11. #include "portab.h"
  12. /**
  13. * @file
  14. * Configuration management (reading, parsing & validation)
  15. */
  16. #include "imp.h"
  17. #include <assert.h>
  18. #include <ctype.h>
  19. #include <errno.h>
  20. #ifdef PROTOTYPES
  21. # include <stdarg.h>
  22. #else
  23. # include <varargs.h>
  24. #endif
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <strings.h>
  29. #include <unistd.h>
  30. #include <pwd.h>
  31. #include <grp.h>
  32. #include <sys/types.h>
  33. #include <unistd.h>
  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 char Conf_MotdFile[FNAME_LEN];
  48. static void Set_Defaults PARAMS(( bool InitServers ));
  49. static bool Read_Config PARAMS(( bool TestOnly, bool IsStarting ));
  50. static bool Validate_Config PARAMS(( bool TestOnly, bool Rehash ));
  51. static void Handle_GLOBAL PARAMS(( int Line, char *Var, char *Arg ));
  52. static void Handle_LIMITS PARAMS(( int Line, char *Var, char *Arg ));
  53. static void Handle_OPTIONS 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_Section PARAMS(( const int Line, const char *Item,
  60. const char *Section ));
  61. static void Config_Error_TooLong PARAMS(( const int LINE, const char *Value ));
  62. static void Init_Server_Struct PARAMS(( CONF_SERVER *Server ));
  63. #ifdef WANT_IPV6
  64. #define DEFAULT_LISTEN_ADDRSTR "::,0.0.0.0"
  65. #else
  66. #define DEFAULT_LISTEN_ADDRSTR "0.0.0.0"
  67. #endif
  68. #ifdef SSL_SUPPORT
  69. static void Handle_SSL PARAMS(( int Line, char *Var, char *Ark ));
  70. struct SSLOptions Conf_SSLOptions;
  71. /**
  72. * Initialize SSL configuration.
  73. */
  74. static void
  75. ConfSSL_Init(void)
  76. {
  77. free(Conf_SSLOptions.KeyFile);
  78. Conf_SSLOptions.KeyFile = NULL;
  79. free(Conf_SSLOptions.CertFile);
  80. Conf_SSLOptions.CertFile = NULL;
  81. free(Conf_SSLOptions.DHFile);
  82. Conf_SSLOptions.DHFile = NULL;
  83. array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
  84. array_free(&Conf_SSLOptions.ListenPorts);
  85. }
  86. /**
  87. * Check if the current configuration uses/requires SSL.
  88. *
  89. * @returns true if SSL is used and should be initialized.
  90. */
  91. GLOBAL bool
  92. Conf_SSLInUse(void)
  93. {
  94. int i;
  95. /* SSL listen ports configured? */
  96. if (array_bytes(&Conf_SSLOptions.ListenPorts))
  97. return true;
  98. for (i = 0; i < MAX_SERVERS; i++) {
  99. if (Conf_Server[i].port > 0
  100. && Conf_Server[i].SSLConnect)
  101. return true;
  102. }
  103. return false;
  104. }
  105. /**
  106. * Make sure that a configured file is readable.
  107. *
  108. * Currently, this function is only used for SSL-related options ...
  109. *
  110. * @param Var Configuration variable
  111. * @param Filename Configured filename
  112. */
  113. static void
  114. CheckFileReadable(const char *Var, const char *Filename)
  115. {
  116. FILE *fp;
  117. if (!Filename)
  118. return;
  119. fp = fopen(Filename, "r");
  120. if (fp)
  121. fclose(fp);
  122. else
  123. Config_Error(LOG_ERR, "Can't read \"%s\" (\"%s\"): %s",
  124. Filename, Var, strerror(errno));
  125. }
  126. #endif
  127. /**
  128. * Duplicate string and warn on errors.
  129. *
  130. * @returns Pointer to string on success, NULL otherwise.
  131. */
  132. static char *
  133. strdup_warn(const char *str)
  134. {
  135. char *ptr = strdup(str);
  136. if (!ptr)
  137. Config_Error(LOG_ERR,
  138. "Could not allocate memory for string: %s", str);
  139. return ptr;
  140. }
  141. /**
  142. * Output a comma separated list of ports (integer values).
  143. */
  144. static void
  145. ports_puts(array *a)
  146. {
  147. size_t len;
  148. UINT16 *ports;
  149. len = array_length(a, sizeof(UINT16));
  150. if (len--) {
  151. ports = (UINT16*) array_start(a);
  152. printf("%u", (unsigned int) *ports);
  153. while (len--) {
  154. ports++;
  155. printf(", %u", (unsigned int) *ports);
  156. }
  157. }
  158. putc('\n', stdout);
  159. }
  160. /**
  161. * Parse a comma separated string into an array of port numbers (integers).
  162. */
  163. static void
  164. ports_parse(array *a, int Line, char *Arg)
  165. {
  166. char *ptr;
  167. int port;
  168. UINT16 port16;
  169. array_trunc(a);
  170. ptr = strtok( Arg, "," );
  171. while (ptr) {
  172. ngt_TrimStr(ptr);
  173. port = atoi(ptr);
  174. if (port > 0 && port < 0xFFFF) {
  175. port16 = (UINT16) port;
  176. if (!array_catb(a, (char*)&port16, sizeof port16))
  177. Config_Error(LOG_ERR, "%s, line %d Could not add port number %ld: %s",
  178. NGIRCd_ConfFile, Line, port, strerror(errno));
  179. } else {
  180. Config_Error( LOG_ERR, "%s, line %d (section \"Global\"): Illegal port number %ld!",
  181. NGIRCd_ConfFile, Line, port );
  182. }
  183. ptr = strtok( NULL, "," );
  184. }
  185. }
  186. /**
  187. * Initialize configuration module.
  188. */
  189. GLOBAL void
  190. Conf_Init( void )
  191. {
  192. Read_Config(false, true);
  193. Validate_Config(false, false);
  194. }
  195. /**
  196. * "Rehash" (reload) server configuration.
  197. *
  198. * @returns true if configuration has been re-read, false on errors.
  199. */
  200. GLOBAL bool
  201. Conf_Rehash( void )
  202. {
  203. if (!Read_Config(false, false))
  204. return false;
  205. Validate_Config(false, true);
  206. /* Update CLIENT structure of local server */
  207. Client_SetInfo(Client_ThisServer(), Conf_ServerInfo);
  208. return true;
  209. }
  210. /**
  211. * Output a boolean value as "yes/no" string.
  212. */
  213. static const char*
  214. yesno_to_str(int boolean_value)
  215. {
  216. if (boolean_value)
  217. return "yes";
  218. return "no";
  219. }
  220. /**
  221. * Free all IRC operator configuration structures.
  222. */
  223. static void
  224. opers_free(void)
  225. {
  226. struct Conf_Oper *op;
  227. size_t len;
  228. len = array_length(&Conf_Opers, sizeof(*op));
  229. op = array_start(&Conf_Opers);
  230. while (len--) {
  231. free(op->mask);
  232. op++;
  233. }
  234. array_free(&Conf_Opers);
  235. }
  236. /**
  237. * Output all IRC operator configuration structures.
  238. */
  239. static void
  240. opers_puts(void)
  241. {
  242. struct Conf_Oper *op;
  243. size_t count, i;
  244. count = array_length(&Conf_Opers, sizeof(*op));
  245. op = array_start(&Conf_Opers);
  246. for (i = 0; i < count; i++, op++) {
  247. if (!op->name[0])
  248. continue;
  249. puts("[OPERATOR]");
  250. printf(" Name = %s\n", op->name);
  251. printf(" Password = %s\n", op->pwd);
  252. printf(" Mask = %s\n\n", op->mask ? op->mask : "");
  253. }
  254. }
  255. /**
  256. * Read configuration, validate and output it.
  257. *
  258. * This function waits for a keypress of the user when stdin/stdout are valid
  259. * tty's ("you can read our nice message and we can read in your keypress").
  260. *
  261. * @return 0 on succes, 1 on failure(s); therefore the result code can
  262. * directly be used by exit() when running "ngircd --configtest".
  263. */
  264. GLOBAL int
  265. Conf_Test( void )
  266. {
  267. struct passwd *pwd;
  268. struct group *grp;
  269. unsigned int i;
  270. bool config_valid;
  271. size_t predef_channel_count;
  272. struct Conf_Channel *predef_chan;
  273. Use_Log = false;
  274. if (!Read_Config(true, true))
  275. return 1;
  276. config_valid = Validate_Config(true, false);
  277. /* Valid tty? */
  278. if(isatty(fileno(stdin)) && isatty(fileno(stdout))) {
  279. puts("OK, press enter to see a dump of your server configuration ...");
  280. getchar();
  281. } else
  282. puts("Ok, dump of your server configuration follows:\n");
  283. puts("[GLOBAL]");
  284. printf(" Name = %s\n", Conf_ServerName);
  285. printf(" AdminInfo1 = %s\n", Conf_ServerAdmin1);
  286. printf(" AdminInfo2 = %s\n", Conf_ServerAdmin2);
  287. printf(" AdminEMail = %s\n", Conf_ServerAdminMail);
  288. printf(" Info = %s\n", Conf_ServerInfo);
  289. printf(" Listen = %s\n", Conf_ListenAddress);
  290. if (Using_MotdFile) {
  291. printf(" MotdFile = %s\n", Conf_MotdFile);
  292. printf(" MotdPhrase =\n");
  293. } else {
  294. printf(" MotdFile = \n");
  295. printf(" MotdPhrase = %s\n", array_bytes(&Conf_Motd)
  296. ? (const char*) array_start(&Conf_Motd) : "");
  297. }
  298. #ifndef PAM
  299. printf(" Password = %s\n", Conf_ServerPwd);
  300. #endif
  301. printf(" PidFile = %s\n", Conf_PidFile);
  302. printf(" Ports = ");
  303. ports_puts(&Conf_ListenPorts);
  304. grp = getgrgid(Conf_GID);
  305. if (grp)
  306. printf(" ServerGID = %s\n", grp->gr_name);
  307. else
  308. printf(" ServerGID = %ld\n", (long)Conf_GID);
  309. pwd = getpwuid(Conf_UID);
  310. if (pwd)
  311. printf(" ServerUID = %s\n", pwd->pw_name);
  312. else
  313. printf(" ServerUID = %ld\n", (long)Conf_UID);
  314. puts("");
  315. puts("[LIMITS]");
  316. printf(" ConnectRetry = %d\n", Conf_ConnectRetry);
  317. printf(" MaxConnections = %d\n", Conf_MaxConnections);
  318. printf(" MaxConnectionsIP = %d\n", Conf_MaxConnectionsIP);
  319. printf(" MaxJoins = %d\n", Conf_MaxJoins > 0 ? Conf_MaxJoins : -1);
  320. printf(" MaxNickLength = %u\n", Conf_MaxNickLength - 1);
  321. printf(" MaxListSize = %d\n", Conf_MaxListSize);
  322. printf(" PingTimeout = %d\n", Conf_PingTimeout);
  323. printf(" PongTimeout = %d\n", Conf_PongTimeout);
  324. puts("");
  325. puts("[OPTIONS]");
  326. printf(" AllowRemoteOper = %s\n", yesno_to_str(Conf_AllowRemoteOper));
  327. printf(" ChrootDir = %s\n", Conf_Chroot);
  328. printf(" CloakHost = %s\n", Conf_CloakHost);
  329. printf(" CloakHostModeX = %s\n", Conf_CloakHostModeX);
  330. printf(" CloakHostSalt = %s\n", Conf_CloakHostSalt);
  331. printf(" CloakUserToNick = %s\n", yesno_to_str(Conf_CloakUserToNick));
  332. #ifdef WANT_IPV6
  333. printf(" ConnectIPv4 = %s\n", yesno_to_str(Conf_ConnectIPv6));
  334. printf(" ConnectIPv6 = %s\n", yesno_to_str(Conf_ConnectIPv4));
  335. #endif
  336. printf(" DNS = %s\n", yesno_to_str(Conf_DNS));
  337. #ifdef IDENT
  338. printf(" Ident = %s\n", yesno_to_str(Conf_Ident));
  339. #endif
  340. printf(" MorePrivacy = %s\n", yesno_to_str(Conf_MorePrivacy));
  341. printf(" NoticeAuth = %s\n", yesno_to_str(Conf_NoticeAuth));
  342. printf(" OperCanUseMode = %s\n", yesno_to_str(Conf_OperCanMode));
  343. printf(" OperChanPAutoOp = %s\n", yesno_to_str(Conf_OperChanPAutoOp));
  344. printf(" OperServerMode = %s\n", yesno_to_str(Conf_OperServerMode));
  345. #ifdef PAM
  346. printf(" PAM = %s\n", yesno_to_str(Conf_PAM));
  347. printf(" PAMIsOptional = %s\n", yesno_to_str(Conf_PAMIsOptional));
  348. #endif
  349. printf(" PredefChannelsOnly = %s\n", yesno_to_str(Conf_PredefChannelsOnly));
  350. #ifndef STRICT_RFC
  351. printf(" RequireAuthPing = %s\n", yesno_to_str(Conf_AuthPing));
  352. #endif
  353. printf(" ScrubCTCP = %s\n", yesno_to_str(Conf_ScrubCTCP));
  354. #ifdef SYSLOG
  355. printf(" SyslogFacility = %s\n",
  356. ngt_SyslogFacilityName(Conf_SyslogFacility));
  357. #endif
  358. printf(" WebircPassword = %s\n", Conf_WebircPwd);
  359. puts("");
  360. #ifdef SSL_SUPPORT
  361. puts("[SSL]");
  362. printf(" CertFile = %s\n", Conf_SSLOptions.CertFile
  363. ? Conf_SSLOptions.CertFile : "");
  364. printf(" DHFile = %s\n", Conf_SSLOptions.DHFile
  365. ? Conf_SSLOptions.DHFile : "");
  366. printf(" KeyFile = %s\n", Conf_SSLOptions.KeyFile
  367. ? Conf_SSLOptions.KeyFile : "");
  368. if (array_bytes(&Conf_SSLOptions.KeyFilePassword))
  369. puts(" KeyFilePassword = <secret>");
  370. else
  371. puts(" KeyFilePassword = ");
  372. array_free_wipe(&Conf_SSLOptions.KeyFilePassword);
  373. printf(" Ports = ");
  374. ports_puts(&Conf_SSLOptions.ListenPorts);
  375. puts("");
  376. #endif
  377. opers_puts();
  378. for( i = 0; i < MAX_SERVERS; i++ ) {
  379. if( ! Conf_Server[i].name[0] ) continue;
  380. /* Valid "Server" section */
  381. puts( "[SERVER]" );
  382. printf( " Name = %s\n", Conf_Server[i].name );
  383. printf( " Host = %s\n", Conf_Server[i].host );
  384. printf( " Port = %u\n", (unsigned int)Conf_Server[i].port );
  385. #ifdef SSL_SUPPORT
  386. printf( " SSLConnect = %s\n", Conf_Server[i].SSLConnect?"yes":"no");
  387. #endif
  388. printf( " MyPassword = %s\n", Conf_Server[i].pwd_in );
  389. printf( " PeerPassword = %s\n", Conf_Server[i].pwd_out );
  390. printf( " ServiceMask = %s\n", Conf_Server[i].svs_mask);
  391. printf( " Group = %d\n", Conf_Server[i].group );
  392. printf( " Passive = %s\n\n", Conf_Server[i].flags & CONF_SFLAG_DISABLED ? "yes" : "no");
  393. }
  394. predef_channel_count = array_length(&Conf_Channels, sizeof(*predef_chan));
  395. predef_chan = array_start(&Conf_Channels);
  396. for (i = 0; i < predef_channel_count; i++, predef_chan++) {
  397. if (!predef_chan->name[0])
  398. continue;
  399. /* Valid "Channel" section */
  400. puts( "[CHANNEL]" );
  401. printf(" Name = %s\n", predef_chan->name);
  402. printf(" Modes = %s\n", predef_chan->modes);
  403. printf(" Key = %s\n", predef_chan->key);
  404. printf(" MaxUsers = %lu\n", predef_chan->maxusers);
  405. printf(" Topic = %s\n", predef_chan->topic);
  406. printf(" KeyFile = %s\n\n", predef_chan->keyfile);
  407. }
  408. return (config_valid ? 0 : 1);
  409. }
  410. /**
  411. * Remove connection information from configured server.
  412. *
  413. * If the server is set as "once", delete it from our configuration;
  414. * otherwise set the time for the next connection attempt.
  415. *
  416. * Non-server connections will be silently ignored.
  417. */
  418. GLOBAL void
  419. Conf_UnsetServer( CONN_ID Idx )
  420. {
  421. int i;
  422. time_t t;
  423. /* Check all our configured servers */
  424. for( i = 0; i < MAX_SERVERS; i++ ) {
  425. if( Conf_Server[i].conn_id != Idx ) continue;
  426. /* Gotcha! Mark server configuration as "unused": */
  427. Conf_Server[i].conn_id = NONE;
  428. if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
  429. /* Delete configuration here */
  430. Init_Server_Struct( &Conf_Server[i] );
  431. } else {
  432. /* Set time for next connect attempt */
  433. t = time(NULL);
  434. if (Conf_Server[i].lasttry < t - Conf_ConnectRetry) {
  435. /* The connection has been "long", so we don't
  436. * require the next attempt to be delayed. */
  437. Conf_Server[i].lasttry =
  438. t - Conf_ConnectRetry + RECONNECT_DELAY;
  439. } else {
  440. /* "Short" connection, enforce "ConnectRetry"
  441. * but randomize it a little bit: 15 seconds. */
  442. Conf_Server[i].lasttry =
  443. t + rand() / (RAND_MAX / 15);
  444. }
  445. }
  446. }
  447. }
  448. /**
  449. * Set connection information for specified configured server.
  450. */
  451. GLOBAL bool
  452. Conf_SetServer( int ConfServer, CONN_ID Idx )
  453. {
  454. assert( ConfServer > NONE );
  455. assert( Idx > NONE );
  456. if (Conf_Server[ConfServer].conn_id > NONE &&
  457. Conf_Server[ConfServer].conn_id != Idx) {
  458. Log(LOG_ERR,
  459. "Connection %d: Server configuration of \"%s\" already in use by connection %d!",
  460. Idx, Conf_Server[ConfServer].name,
  461. Conf_Server[ConfServer].conn_id);
  462. Conn_Close(Idx, NULL, "Server configuration already in use", true);
  463. return false;
  464. }
  465. Conf_Server[ConfServer].conn_id = Idx;
  466. return true;
  467. }
  468. /**
  469. * Get index of server in configuration structure.
  470. */
  471. GLOBAL int
  472. Conf_GetServer( CONN_ID Idx )
  473. {
  474. int i = 0;
  475. assert( Idx > NONE );
  476. for( i = 0; i < MAX_SERVERS; i++ ) {
  477. if( Conf_Server[i].conn_id == Idx ) return i;
  478. }
  479. return NONE;
  480. }
  481. /**
  482. * Enable a server by name and adjust its port number.
  483. *
  484. * @returns true if a server has been enabled and now has a valid port
  485. * number and host name for outgoing connections.
  486. */
  487. GLOBAL bool
  488. Conf_EnableServer( const char *Name, UINT16 Port )
  489. {
  490. int i;
  491. assert( Name != NULL );
  492. for( i = 0; i < MAX_SERVERS; i++ ) {
  493. if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
  494. /* Gotcha! Set port and enable server: */
  495. Conf_Server[i].port = Port;
  496. Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
  497. return (Conf_Server[i].port && Conf_Server[i].host[0]);
  498. }
  499. }
  500. return false;
  501. }
  502. /**
  503. * Enable a server by name.
  504. *
  505. * The server is only usable as outgoing server, if it has set a valid port
  506. * number for outgoing connections!
  507. * If not, you have to use Conf_EnableServer() function to make it available.
  508. *
  509. * @returns true if a server has been enabled; false otherwise.
  510. */
  511. GLOBAL bool
  512. Conf_EnablePassiveServer(const char *Name)
  513. {
  514. int i;
  515. assert( Name != NULL );
  516. for (i = 0; i < MAX_SERVERS; i++) {
  517. if ((strcasecmp( Conf_Server[i].name, Name ) == 0)
  518. && (Conf_Server[i].port > 0)) {
  519. /* BINGO! Enable server */
  520. Conf_Server[i].flags &= ~CONF_SFLAG_DISABLED;
  521. return true;
  522. }
  523. }
  524. return false;
  525. }
  526. /**
  527. * Disable a server by name.
  528. * An already established connection will be disconnected.
  529. *
  530. * @returns true if a server was found and has been disabled.
  531. */
  532. GLOBAL bool
  533. Conf_DisableServer( const char *Name )
  534. {
  535. int i;
  536. assert( Name != NULL );
  537. for( i = 0; i < MAX_SERVERS; i++ ) {
  538. if( strcasecmp( Conf_Server[i].name, Name ) == 0 ) {
  539. /* Gotcha! Disable and disconnect server: */
  540. Conf_Server[i].flags |= CONF_SFLAG_DISABLED;
  541. if( Conf_Server[i].conn_id > NONE )
  542. Conn_Close(Conf_Server[i].conn_id, NULL,
  543. "Server link terminated on operator request",
  544. true);
  545. return true;
  546. }
  547. }
  548. return false;
  549. }
  550. /**
  551. * Add a new remote server to our configuration.
  552. *
  553. * @param Name Name of the new server.
  554. * @param Port Port number to connect to or 0 for incoming connections.
  555. * @param Host Host name to connect to.
  556. * @param MyPwd Password that will be sent to the peer.
  557. * @param PeerPwd Password that must be received from the peer.
  558. * @returns true if the new server has been added; false otherwise.
  559. */
  560. GLOBAL bool
  561. Conf_AddServer(const char *Name, UINT16 Port, const char *Host,
  562. const char *MyPwd, const char *PeerPwd)
  563. {
  564. int i;
  565. assert( Name != NULL );
  566. assert( Host != NULL );
  567. assert( MyPwd != NULL );
  568. assert( PeerPwd != NULL );
  569. /* Search unused item in server configuration structure */
  570. for( i = 0; i < MAX_SERVERS; i++ ) {
  571. /* Is this item used? */
  572. if( ! Conf_Server[i].name[0] ) break;
  573. }
  574. if( i >= MAX_SERVERS ) return false;
  575. Init_Server_Struct( &Conf_Server[i] );
  576. strlcpy( Conf_Server[i].name, Name, sizeof( Conf_Server[i].name ));
  577. strlcpy( Conf_Server[i].host, Host, sizeof( Conf_Server[i].host ));
  578. strlcpy( Conf_Server[i].pwd_out, MyPwd, sizeof( Conf_Server[i].pwd_out ));
  579. strlcpy( Conf_Server[i].pwd_in, PeerPwd, sizeof( Conf_Server[i].pwd_in ));
  580. Conf_Server[i].port = Port;
  581. Conf_Server[i].flags = CONF_SFLAG_ONCE;
  582. return true;
  583. }
  584. /**
  585. * Check if the given nickname is reserved for services on a particular server.
  586. *
  587. * @param ConfServer The server index to check.
  588. * @param Nick The nickname to check.
  589. * @returns true if the given nickname belongs to an "IRC service".
  590. */
  591. GLOBAL bool
  592. Conf_NickIsService(int ConfServer, const char *Nick)
  593. {
  594. assert (ConfServer >= 0);
  595. assert (ConfServer < MAX_SERVERS);
  596. return MatchCaseInsensitiveList(Conf_Server[ConfServer].svs_mask,
  597. Nick, ",");
  598. }
  599. /**
  600. * Check if the given nickname is blocked for "normal client" use.
  601. *
  602. * @param ConfServer The server index or NONE to check all configured servers.
  603. * @param Nick The nickname to check.
  604. * @returns true if the given nickname belongs to an "IRC service".
  605. */
  606. GLOBAL bool
  607. Conf_NickIsBlocked(const char *Nick)
  608. {
  609. int i;
  610. for(i = 0; i < MAX_SERVERS; i++) {
  611. if (!Conf_Server[i].name[0])
  612. continue;
  613. if (Conf_NickIsService(i, Nick))
  614. return true;
  615. }
  616. return false;
  617. }
  618. /**
  619. * Initialize configuration settings with their default values.
  620. */
  621. static void
  622. Set_Defaults(bool InitServers)
  623. {
  624. int i;
  625. char random[RANDOM_SALT_LEN + 1];
  626. /* Global */
  627. strcpy(Conf_ServerName, "");
  628. strcpy(Conf_ServerAdmin1, "");
  629. strcpy(Conf_ServerAdmin2, "");
  630. strcpy(Conf_ServerAdminMail, "");
  631. snprintf(Conf_ServerInfo, sizeof Conf_ServerInfo, "%s %s",
  632. PACKAGE_NAME, PACKAGE_VERSION);
  633. free(Conf_ListenAddress);
  634. Conf_ListenAddress = NULL;
  635. array_free(&Conf_ListenPorts);
  636. array_free(&Conf_Motd);
  637. strlcpy(Conf_MotdFile, SYSCONFDIR, sizeof(Conf_MotdFile));
  638. strlcat(Conf_MotdFile, MOTD_FILE, sizeof(Conf_MotdFile));
  639. strcpy(Conf_ServerPwd, "");
  640. strlcpy(Conf_PidFile, PID_FILE, sizeof(Conf_PidFile));
  641. Conf_UID = Conf_GID = 0;
  642. /* Limits */
  643. Conf_ConnectRetry = 60;
  644. Conf_MaxConnections = 0;
  645. Conf_MaxConnectionsIP = 5;
  646. Conf_MaxJoins = 10;
  647. Conf_MaxNickLength = CLIENT_NICK_LEN_DEFAULT;
  648. Conf_MaxListSize = 100;
  649. Conf_PingTimeout = 120;
  650. Conf_PongTimeout = 20;
  651. /* Options */
  652. Conf_AllowRemoteOper = false;
  653. #ifndef STRICT_RFC
  654. Conf_AuthPing = false;
  655. #endif
  656. strlcpy(Conf_Chroot, CHROOT_DIR, sizeof(Conf_Chroot));
  657. strcpy(Conf_CloakHost, "");
  658. strcpy(Conf_CloakHostModeX, "");
  659. strlcpy(Conf_CloakHostSalt, ngt_RandomStr(random, RANDOM_SALT_LEN),
  660. sizeof(Conf_CloakHostSalt));
  661. Conf_CloakUserToNick = false;
  662. Conf_ConnectIPv4 = true;
  663. #ifdef WANT_IPV6
  664. Conf_ConnectIPv6 = true;
  665. #else
  666. Conf_ConnectIPv6 = false;
  667. #endif
  668. Conf_DNS = true;
  669. #ifdef IDENTAUTH
  670. Conf_Ident = true;
  671. #else
  672. Conf_Ident = false;
  673. #endif
  674. Conf_MorePrivacy = false;
  675. Conf_NoticeAuth = false;
  676. Conf_OperCanMode = false;
  677. Conf_OperChanPAutoOp = true;
  678. Conf_OperServerMode = false;
  679. #ifdef PAM
  680. Conf_PAM = true;
  681. #else
  682. Conf_PAM = false;
  683. #endif
  684. Conf_PAMIsOptional = false;
  685. Conf_PredefChannelsOnly = false;
  686. #ifdef SYSLOG
  687. Conf_ScrubCTCP = false;
  688. #ifdef LOG_LOCAL5
  689. Conf_SyslogFacility = LOG_LOCAL5;
  690. #else
  691. Conf_SyslogFacility = 0;
  692. #endif
  693. #endif
  694. /* Initialize server configuration structures */
  695. if (InitServers) {
  696. for (i = 0; i < MAX_SERVERS;
  697. Init_Server_Struct(&Conf_Server[i++]));
  698. }
  699. }
  700. /**
  701. * Get number of configured listening ports.
  702. *
  703. * @returns The number of ports (IPv4+IPv6) on which the server should listen.
  704. */
  705. static bool
  706. no_listenports(void)
  707. {
  708. size_t cnt = array_bytes(&Conf_ListenPorts);
  709. #ifdef SSL_SUPPORT
  710. cnt += array_bytes(&Conf_SSLOptions.ListenPorts);
  711. #endif
  712. return cnt == 0;
  713. }
  714. /**
  715. * Read MOTD ("message of the day") file.
  716. *
  717. * @param filename Name of the file to read.
  718. */
  719. static void
  720. Read_Motd(const char *filename)
  721. {
  722. char line[127];
  723. FILE *fp;
  724. if (*filename == '\0')
  725. return;
  726. fp = fopen(filename, "r");
  727. if (!fp) {
  728. Config_Error(LOG_WARNING, "Can't read MOTD file \"%s\": %s",
  729. filename, strerror(errno));
  730. return;
  731. }
  732. array_free(&Conf_Motd);
  733. Using_MotdFile = true;
  734. while (fgets(line, (int)sizeof line, fp)) {
  735. ngt_TrimLastChr( line, '\n');
  736. /* add text including \0 */
  737. if (!array_catb(&Conf_Motd, line, strlen(line) + 1)) {
  738. Log(LOG_WARNING, "Cannot add MOTD text: %s", strerror(errno));
  739. break;
  740. }
  741. }
  742. fclose(fp);
  743. }
  744. /**
  745. * Read ngIRCd configuration file.
  746. *
  747. * Please note that this function uses exit(1) on fatal errors and therefore
  748. * can result in ngIRCd terminating!
  749. *
  750. * @param ngircd_starting Flag indicating if ngIRCd is starting or not.
  751. * @returns true when the configuration file has been read
  752. * successfully; false otherwise.
  753. */
  754. static bool
  755. Read_Config(bool TestOnly, bool IsStarting)
  756. {
  757. char section[LINE_LEN], str[LINE_LEN], *var, *arg, *ptr;
  758. const UINT16 defaultport = 6667;
  759. int line, i, n;
  760. size_t count;
  761. FILE *fd;
  762. /* Open configuration file */
  763. fd = fopen( NGIRCd_ConfFile, "r" );
  764. if( ! fd ) {
  765. /* No configuration file found! */
  766. Config_Error( LOG_ALERT, "Can't read configuration \"%s\": %s",
  767. NGIRCd_ConfFile, strerror( errno ));
  768. if (!IsStarting)
  769. return false;
  770. Config_Error( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
  771. exit( 1 );
  772. }
  773. opers_free();
  774. Set_Defaults(IsStarting);
  775. if (TestOnly)
  776. Config_Error(LOG_INFO,
  777. "Reading configuration from \"%s\" ...",
  778. NGIRCd_ConfFile );
  779. /* Clean up server configuration structure: mark all already
  780. * configured servers as "once" so that they are deleted
  781. * after the next disconnect and delete all unused servers.
  782. * And delete all servers which are "duplicates" of servers
  783. * that are already marked as "once" (such servers have been
  784. * created by the last rehash but are now useless). */
  785. for( i = 0; i < MAX_SERVERS; i++ ) {
  786. if( Conf_Server[i].conn_id == NONE ) Init_Server_Struct( &Conf_Server[i] );
  787. else {
  788. /* This structure is in use ... */
  789. if( Conf_Server[i].flags & CONF_SFLAG_ONCE ) {
  790. /* Check for duplicates */
  791. for( n = 0; n < MAX_SERVERS; n++ ) {
  792. if( n == i ) continue;
  793. if( Conf_Server[i].conn_id == Conf_Server[n].conn_id ) {
  794. Init_Server_Struct( &Conf_Server[n] );
  795. #ifdef DEBUG
  796. Log(LOG_DEBUG,"Deleted unused duplicate server %d (kept %d).",
  797. n, i );
  798. #endif
  799. }
  800. }
  801. } else {
  802. /* Mark server as "once" */
  803. Conf_Server[i].flags |= CONF_SFLAG_ONCE;
  804. Log( LOG_DEBUG, "Marked server %d as \"once\"", i );
  805. }
  806. }
  807. }
  808. /* Initialize variables */
  809. line = 0;
  810. strcpy( section, "" );
  811. Init_Server_Struct( &New_Server );
  812. New_Server_Idx = NONE;
  813. #ifdef SSL_SUPPORT
  814. ConfSSL_Init();
  815. #endif
  816. /* Read configuration file */
  817. while( true ) {
  818. if( ! fgets( str, LINE_LEN, fd )) break;
  819. ngt_TrimStr( str );
  820. line++;
  821. /* Skip comments and empty lines */
  822. if( str[0] == ';' || str[0] == '#' || str[0] == '\0' ) continue;
  823. /* Is this the beginning of a new section? */
  824. if(( str[0] == '[' ) && ( str[strlen( str ) - 1] == ']' )) {
  825. strlcpy( section, str, sizeof( section ));
  826. if (strcasecmp(section, "[GLOBAL]") == 0
  827. || strcasecmp(section, "[LIMITS]") == 0
  828. || strcasecmp(section, "[OPTIONS]") == 0
  829. #ifdef SSL_SUPPORT
  830. || strcasecmp(section, "[SSL]") == 0
  831. #endif
  832. )
  833. continue;
  834. if( strcasecmp( section, "[SERVER]" ) == 0 ) {
  835. /* Check if there is already a server to add */
  836. if( New_Server.name[0] ) {
  837. /* Copy data to "real" server structure */
  838. assert( New_Server_Idx > NONE );
  839. Conf_Server[New_Server_Idx] = New_Server;
  840. }
  841. /* Re-init structure for new server */
  842. Init_Server_Struct( &New_Server );
  843. /* Search unused item in server configuration structure */
  844. for( i = 0; i < MAX_SERVERS; i++ ) {
  845. /* Is this item used? */
  846. if( ! Conf_Server[i].name[0] ) break;
  847. }
  848. if( i >= MAX_SERVERS ) {
  849. /* Oops, no free item found! */
  850. Config_Error( LOG_ERR, "Too many servers configured." );
  851. New_Server_Idx = NONE;
  852. }
  853. else New_Server_Idx = i;
  854. continue;
  855. }
  856. if (strcasecmp(section, "[CHANNEL]") == 0) {
  857. count = array_length(&Conf_Channels,
  858. sizeof(struct Conf_Channel));
  859. if (!array_alloc(&Conf_Channels,
  860. sizeof(struct Conf_Channel),
  861. count)) {
  862. Config_Error(LOG_ERR,
  863. "Could not allocate memory for new operator (line %d)",
  864. line);
  865. }
  866. continue;
  867. }
  868. if (strcasecmp(section, "[OPERATOR]") == 0) {
  869. count = array_length(&Conf_Opers,
  870. sizeof(struct Conf_Oper));
  871. if (!array_alloc(&Conf_Opers,
  872. sizeof(struct Conf_Oper),
  873. count)) {
  874. Config_Error(LOG_ERR,
  875. "Could not allocate memory for new channel (line &d)",
  876. line);
  877. }
  878. continue;
  879. }
  880. Config_Error(LOG_ERR,
  881. "%s, line %d: Unknown section \"%s\"!",
  882. NGIRCd_ConfFile, line, section);
  883. section[0] = 0x1;
  884. }
  885. if( section[0] == 0x1 ) continue;
  886. /* Split line into variable name and parameters */
  887. ptr = strchr( str, '=' );
  888. if( ! ptr ) {
  889. Config_Error( LOG_ERR, "%s, line %d: Syntax error!", NGIRCd_ConfFile, line );
  890. continue;
  891. }
  892. *ptr = '\0';
  893. var = str; ngt_TrimStr( var );
  894. arg = ptr + 1; ngt_TrimStr( arg );
  895. if(strcasecmp(section, "[GLOBAL]") == 0)
  896. Handle_GLOBAL(line, var, arg);
  897. else if(strcasecmp(section, "[LIMITS]") == 0)
  898. Handle_LIMITS(line, var, arg);
  899. else if(strcasecmp(section, "[OPTIONS]") == 0)
  900. Handle_OPTIONS(line, var, arg);
  901. #ifdef SSL_SUPPORT
  902. else if(strcasecmp(section, "[SSL]") == 0)
  903. Handle_SSL(line, var, arg);
  904. #endif
  905. else if(strcasecmp(section, "[OPERATOR]") == 0)
  906. Handle_OPERATOR(line, var, arg);
  907. else if(strcasecmp(section, "[SERVER]") == 0)
  908. Handle_SERVER(line, var, arg);
  909. else if(strcasecmp(section, "[CHANNEL]") == 0)
  910. Handle_CHANNEL(line, var, arg);
  911. else
  912. Config_Error(LOG_ERR,
  913. "%s, line %d: Variable \"%s\" outside section!",
  914. NGIRCd_ConfFile, line, var);
  915. }
  916. /* Close configuration file */
  917. fclose( fd );
  918. /* Check if there is still a server to add */
  919. if( New_Server.name[0] ) {
  920. /* Copy data to "real" server structure */
  921. assert( New_Server_Idx > NONE );
  922. Conf_Server[New_Server_Idx] = New_Server;
  923. }
  924. /* not a single listening port? Add default. */
  925. if (no_listenports() &&
  926. !array_copyb(&Conf_ListenPorts, (char*) &defaultport, sizeof defaultport))
  927. {
  928. Config_Error(LOG_ALERT, "Could not add default listening Port %u: %s",
  929. (unsigned int) defaultport, strerror(errno));
  930. exit(1);
  931. }
  932. if (!Conf_ListenAddress)
  933. Conf_ListenAddress = strdup_warn(DEFAULT_LISTEN_ADDRSTR);
  934. if (!Conf_ListenAddress) {
  935. Config_Error(LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME);
  936. exit(1);
  937. }
  938. /* No MOTD phrase configured? (re)try motd file. */
  939. if (array_bytes(&Conf_Motd) == 0)
  940. Read_Motd(Conf_MotdFile);
  941. #ifdef SSL_SUPPORT
  942. /* Make sure that all SSL-related files are readable */
  943. CheckFileReadable("CertFile", Conf_SSLOptions.CertFile);
  944. CheckFileReadable("DHFile", Conf_SSLOptions.DHFile);
  945. CheckFileReadable("KeyFile", Conf_SSLOptions.KeyFile);
  946. #endif
  947. return true;
  948. }
  949. /**
  950. * Check whether a string argument is "true" or "false".
  951. *
  952. * @param Arg Input string.
  953. * @returns true if the input string has been parsed as "yes", "true"
  954. * (case insensitive) or a non-zero integer value.
  955. */
  956. static bool
  957. Check_ArgIsTrue(const char *Arg)
  958. {
  959. if (strcasecmp(Arg, "yes") == 0)
  960. return true;
  961. if (strcasecmp(Arg, "true") == 0)
  962. return true;
  963. if (atoi(Arg) != 0)
  964. return true;
  965. return false;
  966. }
  967. /**
  968. * Handle setting of "MaxNickLength".
  969. *
  970. * @param Line Line number in configuration file.
  971. * @raram Arg Input string.
  972. * @returns New configured maximum nickname length.
  973. */
  974. static unsigned int
  975. Handle_MaxNickLength(int Line, const char *Arg)
  976. {
  977. unsigned new;
  978. new = (unsigned) atoi(Arg) + 1;
  979. if (new > CLIENT_NICK_LEN) {
  980. Config_Error(LOG_WARNING,
  981. "%s, line %d: Value of \"MaxNickLength\" exceeds %u!",
  982. NGIRCd_ConfFile, Line, CLIENT_NICK_LEN - 1);
  983. return CLIENT_NICK_LEN;
  984. }
  985. if (new < 2) {
  986. Config_Error(LOG_WARNING,
  987. "%s, line %d: Value of \"MaxNickLength\" must be at least 1!",
  988. NGIRCd_ConfFile, Line);
  989. return 2;
  990. }
  991. return new;
  992. }
  993. /**
  994. * Output a warning messages if IDENT is configured but not compiled in.
  995. */
  996. static void
  997. WarnIdent(int UNUSED Line)
  998. {
  999. #ifndef IDENTAUTH
  1000. if (Conf_Ident) {
  1001. /* user has enabled ident lookups explicitly, but ... */
  1002. Config_Error(LOG_WARNING,
  1003. "%s: line %d: \"Ident = yes\", but ngircd was built without IDENT support!",
  1004. NGIRCd_ConfFile, Line);
  1005. }
  1006. #endif
  1007. }
  1008. /**
  1009. * Output a warning messages if IPv6 is configured but not compiled in.
  1010. */
  1011. static void
  1012. WarnIPv6(int UNUSED Line)
  1013. {
  1014. #ifndef WANT_IPV6
  1015. if (Conf_ConnectIPv6) {
  1016. /* user has enabled IPv6 explicitly, but ... */
  1017. Config_Error(LOG_WARNING,
  1018. "%s: line %d: \"ConnectIPv6 = yes\", but ngircd was built without IPv6 support!",
  1019. NGIRCd_ConfFile, Line);
  1020. }
  1021. #endif
  1022. }
  1023. /**
  1024. * Output a warning messages if PAM is configured but not compiled in.
  1025. */
  1026. static void
  1027. WarnPAM(int UNUSED Line)
  1028. {
  1029. #ifndef PAM
  1030. if (Conf_PAM) {
  1031. Config_Error(LOG_WARNING,
  1032. "%s: line %d: \"PAM = yes\", but ngircd was built without PAM support!",
  1033. NGIRCd_ConfFile, Line);
  1034. }
  1035. #endif
  1036. }
  1037. /**
  1038. * Handle legacy "NoXXX" options in [GLOBAL] section.
  1039. *
  1040. * TODO: This function and support for "NoXXX" could be removed starting
  1041. * with ngIRCd release 19 (one release after marking it "deprecated").
  1042. *
  1043. * @param Var Variable name.
  1044. * @param Arg Argument string.
  1045. * @returns true if a NoXXX option has been processed; false otherwise.
  1046. */
  1047. static bool
  1048. CheckLegacyNoOption(const char *Var, const char *Arg)
  1049. {
  1050. if(strcasecmp(Var, "NoDNS") == 0) {
  1051. Conf_DNS = !Check_ArgIsTrue( Arg );
  1052. return true;
  1053. }
  1054. if (strcasecmp(Var, "NoIdent") == 0) {
  1055. Conf_Ident = !Check_ArgIsTrue(Arg);
  1056. return true;
  1057. }
  1058. if(strcasecmp(Var, "NoPAM") == 0) {
  1059. Conf_PAM = !Check_ArgIsTrue(Arg);
  1060. return true;
  1061. }
  1062. return false;
  1063. }
  1064. /**
  1065. * Handle deprecated legacy options in [GLOBAL] section.
  1066. *
  1067. * TODO: This function and support for these options in the [Global] section
  1068. * could be removed starting with ngIRCd release 19 (one release after
  1069. * marking it "deprecated").
  1070. *
  1071. * @param Var Variable name.
  1072. * @param Arg Argument string.
  1073. * @returns true if a legacy option has been processed; false otherwise.
  1074. */
  1075. static const char*
  1076. CheckLegacyGlobalOption(int Line, char *Var, char *Arg)
  1077. {
  1078. if (strcasecmp(Var, "AllowRemoteOper") == 0
  1079. || strcasecmp(Var, "ChrootDir") == 0
  1080. || strcasecmp(Var, "ConnectIPv4") == 0
  1081. || strcasecmp(Var, "ConnectIPv6") == 0
  1082. || strcasecmp(Var, "OperCanUseMode") == 0
  1083. || strcasecmp(Var, "OperChanPAutoOp") == 0
  1084. || strcasecmp(Var, "OperServerMode") == 0
  1085. || strcasecmp(Var, "PredefChannelsOnly") == 0
  1086. || strcasecmp(Var, "SyslogFacility") == 0
  1087. || strcasecmp(Var, "WebircPassword") == 0) {
  1088. Handle_OPTIONS(Line, Var, Arg);
  1089. return "[Options]";
  1090. }
  1091. if (strcasecmp(Var, "ConnectRetry") == 0
  1092. || strcasecmp(Var, "MaxConnections") == 0
  1093. || strcasecmp(Var, "MaxConnectionsIP") == 0
  1094. || strcasecmp(Var, "MaxJoins") == 0
  1095. || strcasecmp(Var, "MaxNickLength") == 0
  1096. || strcasecmp(Var, "PingTimeout") == 0
  1097. || strcasecmp(Var, "PongTimeout") == 0) {
  1098. Handle_LIMITS(Line, Var, Arg);
  1099. return "[Limits]";
  1100. }
  1101. #ifdef SSL_SUPPORT
  1102. if (strcasecmp(Var, "SSLCertFile") == 0
  1103. || strcasecmp(Var, "SSLDHFile") == 0
  1104. || strcasecmp(Var, "SSLKeyFile") == 0
  1105. || strcasecmp(Var, "SSLKeyFilePassword") == 0
  1106. || strcasecmp(Var, "SSLPorts") == 0) {
  1107. Handle_SSL(Line, Var + 3, Arg);
  1108. return "[SSL]";
  1109. }
  1110. #endif
  1111. return NULL;
  1112. }
  1113. /**
  1114. * Strip "no" prefix of a string.
  1115. *
  1116. * TODO: This function and support for "NoXXX" should be removed starting
  1117. * with ngIRCd release 19! (One release after marking it "deprecated").
  1118. *
  1119. * @param str Pointer to input string starting with "no".
  1120. * @returns New pointer to string without "no" prefix.
  1121. */
  1122. static const char *
  1123. NoNo(const char *str)
  1124. {
  1125. assert(strncasecmp("no", str, 2) == 0 && str[2]);
  1126. return str + 2;
  1127. }
  1128. /**
  1129. * Invert "boolean" string.
  1130. *
  1131. * TODO: This function and support for "NoXXX" should be removed starting
  1132. * with ngIRCd release 19! (One release after marking it "deprecated").
  1133. *
  1134. * @param arg "Boolean" input string.
  1135. * @returns Pointer to inverted "boolean string".
  1136. */
  1137. static const char *
  1138. InvertArg(const char *arg)
  1139. {
  1140. return yesno_to_str(!Check_ArgIsTrue(arg));
  1141. }
  1142. /**
  1143. * Handle variable in [Global] configuration section.
  1144. *
  1145. * @param Line Line numer in configuration file.
  1146. * @param Var Variable name.
  1147. * @param Arg Variable argument.
  1148. */
  1149. static void
  1150. Handle_GLOBAL( int Line, char *Var, char *Arg )
  1151. {
  1152. struct passwd *pwd;
  1153. struct group *grp;
  1154. size_t len;
  1155. const char *section;
  1156. assert(Line > 0);
  1157. assert(Var != NULL);
  1158. assert(Arg != NULL);
  1159. if (strcasecmp(Var, "Name") == 0) {
  1160. len = strlcpy(Conf_ServerName, Arg, sizeof(Conf_ServerName));
  1161. if (len >= sizeof(Conf_ServerName))
  1162. Config_Error_TooLong(Line, Var);
  1163. return;
  1164. }
  1165. if (strcasecmp(Var, "AdminInfo1") == 0) {
  1166. len = strlcpy(Conf_ServerAdmin1, Arg, sizeof(Conf_ServerAdmin1));
  1167. if (len >= sizeof(Conf_ServerAdmin1))
  1168. Config_Error_TooLong(Line, Var);
  1169. return;
  1170. }
  1171. if (strcasecmp(Var, "AdminInfo2") == 0) {
  1172. len = strlcpy(Conf_ServerAdmin2, Arg, sizeof(Conf_ServerAdmin2));
  1173. if (len >= sizeof(Conf_ServerAdmin2))
  1174. Config_Error_TooLong(Line, Var);
  1175. return;
  1176. }
  1177. if (strcasecmp(Var, "AdminEMail") == 0) {
  1178. len = strlcpy(Conf_ServerAdminMail, Arg,
  1179. sizeof(Conf_ServerAdminMail));
  1180. if (len >= sizeof(Conf_ServerAdminMail))
  1181. Config_Error_TooLong(Line, Var);
  1182. return;
  1183. }
  1184. if (strcasecmp(Var, "Info") == 0) {
  1185. len = strlcpy(Conf_ServerInfo, Arg, sizeof(Conf_ServerInfo));
  1186. if (len >= sizeof(Conf_ServerInfo))
  1187. Config_Error_TooLong(Line, Var);
  1188. return;
  1189. }
  1190. if (strcasecmp(Var, "Listen") == 0) {
  1191. if (Conf_ListenAddress) {
  1192. Config_Error(LOG_ERR,
  1193. "Multiple Listen= options, ignoring: %s",
  1194. Arg);
  1195. return;
  1196. }
  1197. Conf_ListenAddress = strdup_warn(Arg);
  1198. /* If allocation fails, we're in trouble: we cannot ignore the
  1199. * error -- otherwise ngircd would listen on all interfaces. */
  1200. if (!Conf_ListenAddress) {
  1201. Config_Error(LOG_ALERT,
  1202. "%s exiting due to fatal errors!",
  1203. PACKAGE_NAME);
  1204. exit(1);
  1205. }
  1206. return;
  1207. }
  1208. if (strcasecmp(Var, "MotdFile") == 0) {
  1209. len = strlcpy(Conf_MotdFile, Arg, sizeof(Conf_MotdFile));
  1210. if (len >= sizeof(Conf_MotdFile))
  1211. Config_Error_TooLong(Line, Var);
  1212. return;
  1213. }
  1214. if (strcasecmp(Var, "MotdPhrase") == 0) {
  1215. len = strlen(Arg);
  1216. if (len == 0)
  1217. return;
  1218. if (len >= LINE_LEN) {
  1219. Config_Error_TooLong(Line, Var);
  1220. return;
  1221. }
  1222. if (!array_copyb(&Conf_Motd, Arg, len + 1))
  1223. Config_Error(LOG_WARNING,
  1224. "%s, line %d: Could not append MotdPhrase: %s",
  1225. NGIRCd_ConfFile, Line, strerror(errno));
  1226. Using_MotdFile = false;
  1227. return;
  1228. }
  1229. if(strcasecmp(Var, "Password") == 0) {
  1230. len = strlcpy(Conf_ServerPwd, Arg, sizeof(Conf_ServerPwd));
  1231. if (len >= sizeof(Conf_ServerPwd))
  1232. Config_Error_TooLong(Line, Var);
  1233. return;
  1234. }
  1235. if (strcasecmp(Var, "PidFile") == 0) {
  1236. len = strlcpy(Conf_PidFile, Arg, sizeof(Conf_PidFile));
  1237. if (len >= sizeof(Conf_PidFile))
  1238. Config_Error_TooLong(Line, Var);
  1239. return;
  1240. }
  1241. if (strcasecmp(Var, "Ports") == 0) {
  1242. ports_parse(&Conf_ListenPorts, Line, Arg);
  1243. return;
  1244. }
  1245. if (strcasecmp(Var, "ServerGID") == 0) {
  1246. grp = getgrnam(Arg);
  1247. if (grp)
  1248. Conf_GID = grp->gr_gid;
  1249. else {
  1250. Conf_GID = (unsigned int)atoi(Arg);
  1251. if (!Conf_GID && strcmp(Arg, "0"))
  1252. Config_Error(LOG_WARNING,
  1253. "%s, line %d: Value of \"%s\" is not a valid group name or ID!",
  1254. NGIRCd_ConfFile, Line, Var);
  1255. }
  1256. return;
  1257. }
  1258. if (strcasecmp(Var, "ServerUID") == 0) {
  1259. pwd = getpwnam(Arg);
  1260. if (pwd)
  1261. Conf_UID = pwd->pw_uid;
  1262. else {
  1263. Conf_UID = (unsigned int)atoi(Arg);
  1264. if (!Conf_UID && strcmp(Arg, "0"))
  1265. Config_Error(LOG_WARNING,
  1266. "%s, line %d: Value of \"%s\" is not a valid user name or ID!",
  1267. NGIRCd_ConfFile, Line, Var);
  1268. }
  1269. return;
  1270. }
  1271. if (CheckLegacyNoOption(Var, Arg)) {
  1272. /* TODO: This function and support for "NoXXX" could be
  1273. * be removed starting with ngIRCd release 19 (one release
  1274. * after marking it "deprecated"). */
  1275. Config_Error(LOG_WARNING,
  1276. "%s, line %d (section \"Global\"): \"No\"-Prefix is deprecated, use \"%s = %s\" in [Options] section!",
  1277. NGIRCd_ConfFile, Line, NoNo(Var), InvertArg(Arg));
  1278. if (strcasecmp(Var, "NoIdent") == 0)
  1279. WarnIdent(Line);
  1280. else if (strcasecmp(Var, "NoPam") == 0)
  1281. WarnPAM(Line);
  1282. return;
  1283. }
  1284. if ((section = CheckLegacyGlobalOption(Line, Var, Arg))) {
  1285. /** TODO: This function and support for these options in the
  1286. * [Global] section could be removed starting with ngIRCd
  1287. * release 19 (one release after marking it "deprecated"). */
  1288. if (strncasecmp(Var, "SSL", 3) == 0) {
  1289. Config_Error(LOG_WARNING,
  1290. "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s and rename to \"%s\"!",
  1291. NGIRCd_ConfFile, Line, Var, section,
  1292. Var + 3);
  1293. } else {
  1294. Config_Error(LOG_WARNING,
  1295. "%s, line %d (section \"Global\"): \"%s\" is deprecated here, move it to %s!",
  1296. NGIRCd_ConfFile, Line, Var, section);
  1297. }
  1298. return;
  1299. }
  1300. Config_Error_Section(Line, Var, "Global");
  1301. }
  1302. /**
  1303. * Handle variable in [Limits] configuration section.
  1304. *
  1305. * @param Line Line numer in configuration file.
  1306. * @param Var Variable name.
  1307. * @param Arg Variable argument.
  1308. */
  1309. static void
  1310. Handle_LIMITS(int Line, char *Var, char *Arg)
  1311. {
  1312. assert(Line > 0);
  1313. assert(Var != NULL);
  1314. assert(Arg != NULL);
  1315. if (strcasecmp(Var, "ConnectRetry") == 0) {
  1316. Conf_ConnectRetry = atoi(Arg);
  1317. if (Conf_ConnectRetry < 5) {
  1318. Config_Error(LOG_WARNING,
  1319. "%s, line %d: Value of \"ConnectRetry\" too low!",
  1320. NGIRCd_ConfFile, Line);
  1321. Conf_ConnectRetry = 5;
  1322. }
  1323. return;
  1324. }
  1325. if (strcasecmp(Var, "MaxConnections") == 0) {
  1326. Conf_MaxConnections = atoi(Arg);
  1327. if (!Conf_MaxConnections && strcmp(Arg, "0"))
  1328. Config_Error_NaN(Line, Var);
  1329. return;
  1330. }
  1331. if (strcasecmp(Var, "MaxConnectionsIP") == 0) {
  1332. Conf_MaxConnectionsIP = atoi(Arg);
  1333. if (!Conf_MaxConnectionsIP && strcmp(Arg, "0"))
  1334. Config_Error_NaN(Line, Var);
  1335. return;
  1336. }
  1337. if (strcasecmp(Var, "MaxJoins") == 0) {
  1338. Conf_MaxJoins = atoi(Arg);
  1339. if (!Conf_MaxJoins && strcmp(Arg, "0"))
  1340. Config_Error_NaN(Line, Var);
  1341. return;
  1342. }
  1343. if (strcasecmp(Var, "MaxNickLength") == 0) {
  1344. Conf_MaxNickLength = Handle_MaxNickLength(Line, Arg);
  1345. return;
  1346. }
  1347. if (strcasecmp(Var, "MaxListSize") == 0) {
  1348. Conf_MaxListSize = atoi(Arg);
  1349. if (!Conf_MaxListSize && strcmp(Arg, "0"))
  1350. Config_Error_NaN(Line, Var);
  1351. return;
  1352. }
  1353. if (strcasecmp(Var, "PingTimeout") == 0) {
  1354. Conf_PingTimeout = atoi(Arg);
  1355. if (Conf_PingTimeout < 5) {
  1356. Config_Error(LOG_WARNING,
  1357. "%s, line %d: Value of \"PingTimeout\" too low!",
  1358. NGIRCd_ConfFile, Line);
  1359. Conf_PingTimeout = 5;
  1360. }
  1361. return;
  1362. }
  1363. if (strcasecmp(Var, "PongTimeout") == 0) {
  1364. Conf_PongTimeout = atoi(Arg);
  1365. if (Conf_PongTimeout < 5) {
  1366. Config_Error(LOG_WARNING,
  1367. "%s, line %d: Value of \"PongTimeout\" too low!",
  1368. NGIRCd_ConfFile, Line);
  1369. Conf_PongTimeout = 5;
  1370. }
  1371. return;
  1372. }
  1373. Config_Error_Section(Line, Var, "Limits");
  1374. }
  1375. /**
  1376. * Handle variable in [Options] configuration section.
  1377. *
  1378. * @param Line Line numer in configuration file.
  1379. * @param Var Variable name.
  1380. * @param Arg Variable argument.
  1381. */
  1382. static void
  1383. Handle_OPTIONS(int Line, char *Var, char *Arg)
  1384. {
  1385. size_t len;
  1386. assert(Line > 0);
  1387. assert(Var != NULL);
  1388. assert(Arg != NULL);
  1389. if (strcasecmp(Var, "AllowRemoteOper") == 0) {
  1390. Conf_AllowRemoteOper = Check_ArgIsTrue(Arg);
  1391. return;
  1392. }
  1393. if (strcasecmp(Var, "ChrootDir") == 0) {
  1394. len = strlcpy(Conf_Chroot, Arg, sizeof(Conf_Chroot));
  1395. if (len >= sizeof(Conf_Chroot))
  1396. Config_Error_TooLong(Line, Var);
  1397. return;
  1398. }
  1399. if (strcasecmp(Var, "CloakHost") == 0) {
  1400. len = strlcpy(Conf_CloakHost, Arg, sizeof(Conf_CloakHost));
  1401. if (len >= sizeof(Conf_CloakHost))
  1402. Config_Error_TooLong(Line, Var);
  1403. return;
  1404. }
  1405. if (strcasecmp(Var, "CloakHostModeX") == 0) {
  1406. len = strlcpy(Conf_CloakHostModeX, Arg, sizeof(Conf_CloakHostModeX));
  1407. if (len >= sizeof(Conf_CloakHostModeX))
  1408. Config_Error_TooLong(Line, Var);
  1409. return;
  1410. }
  1411. if (strcasecmp(Var, "CloakHostSalt") == 0) {
  1412. len = strlcpy(Conf_CloakHostSalt, Arg, sizeof(Conf_CloakHostSalt));
  1413. if (len >= sizeof(Conf_CloakHostSalt))
  1414. Config_Error_TooLong(Line, Var);
  1415. return;
  1416. }
  1417. if (strcasecmp(Var, "CloakUserToNick") == 0) {
  1418. Conf_CloakUserToNick = Check_ArgIsTrue(Arg);
  1419. return;
  1420. }
  1421. if (strcasecmp(Var, "ConnectIPv6") == 0) {
  1422. Conf_ConnectIPv6 = Check_ArgIsTrue(Arg);
  1423. WarnIPv6(Line);
  1424. return;
  1425. }
  1426. if (strcasecmp(Var, "ConnectIPv4") == 0) {
  1427. Conf_ConnectIPv4 = Check_ArgIsTrue(Arg);
  1428. return;
  1429. }
  1430. if (strcasecmp(Var, "DNS") == 0) {
  1431. Conf_DNS = Check_ArgIsTrue(Arg);
  1432. return;
  1433. }
  1434. if (strcasecmp(Var, "Ident") == 0) {
  1435. Conf_Ident = Check_ArgIsTrue(Arg);
  1436. WarnIdent(Line);
  1437. return;
  1438. }
  1439. if (strcasecmp(Var, "MorePrivacy") == 0) {
  1440. Conf_MorePrivacy = Check_ArgIsTrue(Arg);
  1441. return;
  1442. }
  1443. if (strcasecmp(Var, "NoticeAuth") == 0) {
  1444. Conf_NoticeAuth = Check_ArgIsTrue(Arg);
  1445. return;
  1446. }
  1447. if (strcasecmp(Var, "OperCanUseMode") == 0) {
  1448. Conf_OperCanMode = Check_ArgIsTrue(Arg);
  1449. return;
  1450. }
  1451. if (strcasecmp(Var, "OperChanPAutoOp") == 0) {
  1452. Conf_OperChanPAutoOp = Check_ArgIsTrue(Arg);
  1453. return;
  1454. }
  1455. if (strcasecmp(Var, "OperServerMode") == 0) {
  1456. Conf_OperServerMode = Check_ArgIsTrue(Arg);
  1457. return;
  1458. }
  1459. if (strcasecmp(Var, "PAM") == 0) {
  1460. Conf_PAM = Check_ArgIsTrue(Arg);
  1461. WarnPAM(Line);
  1462. return;
  1463. }
  1464. if (strcasecmp(Var, "PAMIsOptional") == 0 ) {
  1465. Conf_PAMIsOptional = Check_ArgIsTrue(Arg);
  1466. return;
  1467. }
  1468. if (strcasecmp(Var, "PredefChannelsOnly") == 0) {
  1469. Conf_PredefChannelsOnly = Check_ArgIsTrue(Arg);
  1470. return;
  1471. }
  1472. #ifndef STRICT_RFC
  1473. if (strcasecmp(Var, "RequireAuthPing") == 0) {
  1474. Conf_AuthPing = Check_ArgIsTrue(Arg);
  1475. return;
  1476. }
  1477. #endif
  1478. if (strcasecmp(Var, "ScrubCTCP") == 0) {
  1479. Conf_ScrubCTCP = Check_ArgIsTrue(Arg);
  1480. return;
  1481. }
  1482. #ifdef SYSLOG
  1483. if (strcasecmp(Var, "SyslogFacility") == 0) {
  1484. Conf_SyslogFacility = ngt_SyslogFacilityID(Arg,
  1485. Conf_SyslogFacility);
  1486. return;
  1487. }
  1488. #endif
  1489. if (strcasecmp(Var, "WebircPassword") == 0) {
  1490. len = strlcpy(Conf_WebircPwd, Arg, sizeof(Conf_WebircPwd));
  1491. if (len >= sizeof(Conf_WebircPwd))
  1492. Config_Error_TooLong(Line, Var);
  1493. return;
  1494. }
  1495. Config_Error_Section(Line, Var, "Options");
  1496. }
  1497. #ifdef SSL_SUPPORT
  1498. /**
  1499. * Handle variable in [SSL] configuration section.
  1500. *
  1501. * @param Line Line numer in configuration file.
  1502. * @param Var Variable name.
  1503. * @param Arg Variable argument.
  1504. */
  1505. static void
  1506. Handle_SSL(int Line, char *Var, char *Arg)
  1507. {
  1508. assert(Line > 0);
  1509. assert(Var != NULL);
  1510. assert(Arg != NULL);
  1511. if (strcasecmp(Var, "CertFile") == 0) {
  1512. assert(Conf_SSLOptions.CertFile == NULL);
  1513. Conf_SSLOptions.CertFile = strdup_warn(Arg);
  1514. return;
  1515. }
  1516. if (strcasecmp(Var, "DHFile") == 0) {
  1517. assert(Conf_SSLOptions.DHFile == NULL);
  1518. Conf_SSLOptions.DHFile = strdup_warn(Arg);
  1519. return;
  1520. }
  1521. if (strcasecmp(Var, "KeyFile") == 0) {
  1522. assert(Conf_SSLOptions.KeyFile == NULL);
  1523. Conf_SSLOptions.KeyFile = strdup_warn(Arg);
  1524. return;
  1525. }
  1526. if (strcasecmp(Var, "KeyFilePassword") == 0) {
  1527. assert(array_bytes(&Conf_SSLOptions.KeyFilePassword) == 0);
  1528. if (!array_copys(&Conf_SSLOptions.KeyFilePassword, Arg))
  1529. Config_Error(LOG_ERR,
  1530. "%s, line %d (section \"SSL\"): Could not copy %s: %s!",
  1531. NGIRCd_ConfFile, Line, Var,
  1532. strerror(errno));
  1533. return;
  1534. }
  1535. if (strcasecmp(Var, "Ports") == 0) {
  1536. ports_parse(&Conf_SSLOptions.ListenPorts, Line, Arg);
  1537. return;
  1538. }
  1539. Config_Error_Section(Line, Var, "SSL");
  1540. }
  1541. #endif
  1542. /**
  1543. * Handle variable in [Operator] configuration section.
  1544. *
  1545. * @param Line Line numer in configuration file.
  1546. * @param Var Variable name.
  1547. * @param Arg Variable argument.
  1548. */
  1549. static void
  1550. Handle_OPERATOR( int Line, char *Var, char *Arg )
  1551. {
  1552. size_t len;
  1553. struct Conf_Oper *op;
  1554. assert( Line > 0 );
  1555. assert( Var != NULL );
  1556. assert( Arg != NULL );
  1557. op = array_get(&Conf_Opers, sizeof(*op),
  1558. array_length(&Conf_Opers, sizeof(*op)) - 1);
  1559. if (!op)
  1560. return;
  1561. if (strcasecmp(Var, "Name") == 0) {
  1562. /* Name of IRC operator */
  1563. len = strlcpy(op->name, Arg, sizeof(op->name));
  1564. if (len >= sizeof(op->name))
  1565. Config_Error_TooLong(Line, Var);
  1566. return;
  1567. }
  1568. if (strcasecmp(Var, "Password") == 0) {
  1569. /* Password of IRC operator */
  1570. len = strlcpy(op->pwd, Arg, sizeof(op->pwd));
  1571. if (len >= sizeof(op->pwd))
  1572. Config_Error_TooLong(Line, Var);
  1573. return;
  1574. }
  1575. if (strcasecmp(Var, "Mask") == 0) {
  1576. if (op->mask)
  1577. return; /* Hostname already configured */
  1578. op->mask = strdup_warn( Arg );
  1579. return;
  1580. }
  1581. Config_Error_Section(Line, Var, "Operator");
  1582. }
  1583. /**
  1584. * Handle variable in [Server] configuration section.
  1585. *
  1586. * @param Line Line numer in configuration file.
  1587. * @param Var Variable name.
  1588. * @param Arg Variable argument.
  1589. */
  1590. static void
  1591. Handle_SERVER( int Line, char *Var, char *Arg )
  1592. {
  1593. long port;
  1594. size_t len;
  1595. assert( Line > 0 );
  1596. assert( Var != NULL );
  1597. assert( Arg != NULL );
  1598. /* Ignore server block if no space is left in server configuration structure */
  1599. if( New_Server_Idx <= NONE ) return;
  1600. if( strcasecmp( Var, "Host" ) == 0 ) {
  1601. /* Hostname of the server */
  1602. len = strlcpy( New_Server.host, Arg, sizeof( New_Server.host ));
  1603. if (len >= sizeof( New_Server.host ))
  1604. Config_Error_TooLong ( Line, Var );
  1605. return;
  1606. }
  1607. if( strcasecmp( Var, "Name" ) == 0 ) {
  1608. /* Name of the server ("Nick"/"ID") */
  1609. len = strlcpy( New_Server.name, Arg, sizeof( New_Server.name ));
  1610. if (len >= sizeof( New_Server.name ))
  1611. Config_Error_TooLong( Line, Var );
  1612. return;
  1613. }
  1614. if (strcasecmp(Var, "Bind") == 0) {
  1615. if (ng_ipaddr_init(&New_Server.bind_addr, Arg, 0))
  1616. return;
  1617. Config_Error(LOG_ERR, "%s, line %d (section \"Server\"): Can't parse IP address \"%s\"",
  1618. NGIRCd_ConfFile, Line, Arg);
  1619. return;
  1620. }
  1621. if( strcasecmp( Var, "MyPassword" ) == 0 ) {
  1622. /* Password of this server which is sent to the peer */
  1623. if (*Arg == ':') {
  1624. Config_Error(LOG_ERR,
  1625. "%s, line %d (section \"Server\"): MyPassword must not start with ':'!",
  1626. NGIRCd_ConfFile, Line);
  1627. }
  1628. len = strlcpy( New_Server.pwd_in, Arg, sizeof( New_Server.pwd_in ));
  1629. if (len >= sizeof( New_Server.pwd_in ))
  1630. Config_Error_TooLong( Line, Var );
  1631. return;
  1632. }
  1633. if( strcasecmp( Var, "PeerPassword" ) == 0 ) {
  1634. /* Passwort of the peer which must be received */
  1635. len = strlcpy( New_Server.pwd_out, Arg, sizeof( New_Server.pwd_out ));
  1636. if (len >= sizeof( New_Server.pwd_out ))
  1637. Config_Error_TooLong( Line, Var );
  1638. return;
  1639. }
  1640. if( strcasecmp( Var, "Port" ) == 0 ) {
  1641. /* Port to which this server should connect */
  1642. port = atol( Arg );
  1643. if (port >= 0 && port < 0xFFFF)
  1644. New_Server.port = (UINT16)port;
  1645. else
  1646. Config_Error(LOG_ERR,
  1647. "%s, line %d (section \"Server\"): Illegal port number %ld!",
  1648. NGIRCd_ConfFile, Line, port );
  1649. return;
  1650. }
  1651. #ifdef SSL_SUPPORT
  1652. if( strcasecmp( Var, "SSLConnect" ) == 0 ) {
  1653. New_Server.SSLConnect = Check_ArgIsTrue(Arg);
  1654. return;
  1655. }
  1656. #endif
  1657. if( strcasecmp( Var, "Group" ) == 0 ) {
  1658. /* Server group */
  1659. New_Server.group = atoi( Arg );
  1660. if (!New_Server.group && strcmp(Arg, "0"))
  1661. Config_Error_NaN(Line, Var);
  1662. return;
  1663. }
  1664. if( strcasecmp( Var, "Passive" ) == 0 ) {
  1665. if (Check_ArgIsTrue(Arg))
  1666. New_Server.flags |= CONF_SFLAG_DISABLED;
  1667. return;
  1668. }
  1669. if (strcasecmp(Var, "ServiceMask") == 0) {
  1670. len = strlcpy(New_Server.svs_mask, ngt_LowerStr(Arg),
  1671. sizeof(New_Server.svs_mask));
  1672. if (len >= sizeof(New_Server.svs_mask))
  1673. Config_Error_TooLong(Line, Var);
  1674. return;
  1675. }
  1676. Config_Error_Section(Line, Var, "Server");
  1677. }
  1678. /**
  1679. * Copy channel name into channel structure.
  1680. *
  1681. * If the channel name is not valid because of a missing prefix ('#', '&'),
  1682. * a default prefix of '#' will be added.
  1683. *
  1684. * @param new_chan New already allocated channel structure.
  1685. * @param name Name of the new channel.
  1686. * @returns true on success, false otherwise.
  1687. */
  1688. static bool
  1689. Handle_Channelname(struct Conf_Channel *new_chan, const char *name)
  1690. {
  1691. size_t size = sizeof(new_chan->name);
  1692. char *dest = new_chan->name;
  1693. if (!Channel_IsValidName(name)) {
  1694. /*
  1695. * maybe user forgot to add a '#'.
  1696. * This is only here for user convenience.
  1697. */
  1698. *dest = '#';
  1699. --size;
  1700. ++dest;
  1701. }
  1702. return size > strlcpy(dest, name, size);
  1703. }
  1704. /**
  1705. * Handle variable in [Channel] configuration section.
  1706. *
  1707. * @param Line Line numer in configuration file.
  1708. * @param Var Variable name.
  1709. * @param Arg Variable argument.
  1710. */
  1711. static void
  1712. Handle_CHANNEL(int Line, char *Var, char *Arg)
  1713. {
  1714. size_t len;
  1715. struct Conf_Channel *chan;
  1716. assert( Line > 0 );
  1717. assert( Var != NULL );
  1718. assert( Arg != NULL );
  1719. chan = array_get(&Conf_Channels, sizeof(*chan),
  1720. array_length(&Conf_Channels, sizeof(*chan)) - 1);
  1721. if (!chan)
  1722. return;
  1723. if (strcasecmp(Var, "Name") == 0) {
  1724. if (!Handle_Channelname(chan, Arg))
  1725. Config_Error_TooLong(Line, Var);
  1726. return;
  1727. }
  1728. if (strcasecmp(Var, "Modes") == 0) {
  1729. /* Initial modes */
  1730. len = strlcpy(chan->modes, Arg, sizeof(chan->modes));
  1731. if (len >= sizeof(chan->modes))
  1732. Config_Error_TooLong( Line, Var );
  1733. return;
  1734. }
  1735. if( strcasecmp( Var, "Topic" ) == 0 ) {
  1736. /* Initial topic */
  1737. len = strlcpy(chan->topic, Arg, sizeof(chan->topic));
  1738. if (len >= sizeof(chan->topic))
  1739. Config_Error_TooLong( Line, Var );
  1740. return;
  1741. }
  1742. if( strcasecmp( Var, "Key" ) == 0 ) {
  1743. /* Initial Channel Key (mode k) */
  1744. len = strlcpy(chan->key, Arg, sizeof(chan->key));
  1745. if (len >= sizeof(chan->key))
  1746. Config_Error_TooLong(Line, Var);
  1747. return;
  1748. }
  1749. if( strcasecmp( Var, "MaxUsers" ) == 0 ) {
  1750. /* maximum user limit, mode l */
  1751. chan->maxusers = (unsigned long) atol(Arg);
  1752. if (!chan->maxusers && strcmp(Arg, "0"))
  1753. Config_Error_NaN(Line, Var);
  1754. return;
  1755. }
  1756. if (strcasecmp(Var, "KeyFile") == 0) {
  1757. /* channel keys */
  1758. len = strlcpy(chan->keyfile, Arg, sizeof(chan->keyfile));
  1759. if (len >= sizeof(chan->keyfile))
  1760. Config_Error_TooLong(Line, Var);
  1761. return;
  1762. }
  1763. Config_Error_Section(Line, Var, "Channel");
  1764. }
  1765. /**
  1766. * Validate server configuration.
  1767. *
  1768. * Please note that this function uses exit(1) on fatal errors and therefore
  1769. * can result in ngIRCd terminating!
  1770. *
  1771. * @param Configtest true if the daemon has been called with "--configtest".
  1772. * @param Rehash true if re-reading configuration on runtime.
  1773. * @returns true if configuration is valid.
  1774. */
  1775. static bool
  1776. Validate_Config(bool Configtest, bool Rehash)
  1777. {
  1778. /* Validate configuration settings. */
  1779. #ifdef DEBUG
  1780. int i, servers, servers_once;
  1781. #endif
  1782. bool config_valid = true;
  1783. char *ptr;
  1784. /* Emit a warning when the config file is not a full path name */
  1785. if (NGIRCd_ConfFile[0] && NGIRCd_ConfFile[0] != '/') {
  1786. Config_Error(LOG_WARNING,
  1787. "Not specifying a full path name to \"%s\" can cause problems when rehashing the server!",
  1788. NGIRCd_ConfFile);
  1789. }
  1790. /* Validate configured server name, see RFC 2812 section 2.3.1 */
  1791. ptr = Conf_ServerName;
  1792. do {
  1793. if (*ptr >= 'a' && *ptr <= 'z') continue;
  1794. if (*ptr >= 'A' && *ptr <= 'Z') continue;
  1795. if (*ptr >= '0' && *ptr <= '9') continue;
  1796. if (ptr > Conf_ServerName) {
  1797. if (*ptr == '.' || *ptr == '-')
  1798. continue;
  1799. }
  1800. Conf_ServerName[0] = '\0';
  1801. break;
  1802. } while (*(++ptr));
  1803. if (!Conf_ServerName[0]) {
  1804. /* No server name configured! */
  1805. config_valid = false;
  1806. Config_Error(LOG_ALERT,
  1807. "No (valid) server name configured in \"%s\" (section 'Global': 'Name')!",
  1808. NGIRCd_ConfFile);
  1809. if (!Configtest && !Rehash) {
  1810. Config_Error(LOG_ALERT,
  1811. "%s exiting due to fatal errors!",
  1812. PACKAGE_NAME);
  1813. exit(1);
  1814. }
  1815. }
  1816. if (Conf_ServerName[0] && !strchr(Conf_ServerName, '.')) {
  1817. /* No dot in server name! */
  1818. config_valid = false;
  1819. Config_Error(LOG_ALERT,
  1820. "Invalid server name configured in \"%s\" (section 'Global': 'Name'): Dot missing!",
  1821. NGIRCd_ConfFile);
  1822. if (!Configtest) {
  1823. Config_Error(LOG_ALERT,
  1824. "%s exiting due to fatal errors!",
  1825. PACKAGE_NAME);
  1826. exit(1);
  1827. }
  1828. }
  1829. #ifdef STRICT_RFC
  1830. if (!Conf_ServerAdminMail[0]) {
  1831. /* No administrative contact configured! */
  1832. config_valid = false;
  1833. Config_Error(LOG_ALERT,
  1834. "No administrator email address configured in \"%s\" ('AdminEMail')!",
  1835. NGIRCd_ConfFile);
  1836. if (!Configtest) {
  1837. Config_Error(LOG_ALERT,
  1838. "%s exiting due to fatal errors!",
  1839. PACKAGE_NAME);
  1840. exit(1);
  1841. }
  1842. }
  1843. #endif
  1844. if (!Conf_ServerAdmin1[0] && !Conf_ServerAdmin2[0]
  1845. && !Conf_ServerAdminMail[0]) {
  1846. /* No administrative information configured! */
  1847. Config_Error(LOG_WARNING,
  1848. "No administrative information configured but required by RFC!");
  1849. }
  1850. #ifdef PAM
  1851. if (Conf_ServerPwd[0])
  1852. Config_Error(LOG_ERR,
  1853. "This server uses PAM, \"Password\" in [Global] section will be ignored!");
  1854. #endif
  1855. #ifdef DEBUG
  1856. servers = servers_once = 0;
  1857. for (i = 0; i < MAX_SERVERS; i++) {
  1858. if (Conf_Server[i].name[0]) {
  1859. servers++;
  1860. if (Conf_Server[i].flags & CONF_SFLAG_ONCE)
  1861. servers_once++;
  1862. }
  1863. }
  1864. Log(LOG_DEBUG,
  1865. "Configuration: Operators=%ld, Servers=%d[%d], Channels=%ld",
  1866. array_length(&Conf_Opers, sizeof(struct Conf_Oper)),
  1867. servers, servers_once,
  1868. array_length(&Conf_Channels, sizeof(struct Conf_Channel)));
  1869. #endif
  1870. return config_valid;
  1871. }
  1872. /**
  1873. * Output "line too long" warning.
  1874. *
  1875. * @param Line Line number in configuration file.
  1876. * @param Item Affected variable name.
  1877. */
  1878. static void
  1879. Config_Error_TooLong ( const int Line, const char *Item )
  1880. {
  1881. Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" too long!", NGIRCd_ConfFile, Line, Item );
  1882. }
  1883. /**
  1884. * Output "unknown variable" warning.
  1885. *
  1886. * @param Line Line number in configuration file.
  1887. * @param Item Affected variable name.
  1888. * @param Section Section name.
  1889. */
  1890. static void
  1891. Config_Error_Section(const int Line, const char *Item, const char *Section)
  1892. {
  1893. Config_Error(LOG_ERR, "%s, line %d (section \"%s\"): Unknown variable \"%s\"!",
  1894. NGIRCd_ConfFile, Line, Section, Item);
  1895. }
  1896. /**
  1897. * Output "not a number" warning.
  1898. *
  1899. * @param Line Line number in configuration file.
  1900. * @param Item Affected variable name.
  1901. */
  1902. static void
  1903. Config_Error_NaN( const int Line, const char *Item )
  1904. {
  1905. Config_Error( LOG_WARNING, "%s, line %d: Value of \"%s\" is not a number!",
  1906. NGIRCd_ConfFile, Line, Item );
  1907. }
  1908. /**
  1909. * Output configuration error to console and/or logfile.
  1910. *
  1911. * On runtime, the normal log functions of the daemon are used. But when
  1912. * testing the configuration ("--configtest"), all messages go directly
  1913. * to the console.
  1914. *
  1915. * @param Level Severity level of the message.
  1916. * @param Format Format string; see printf() function.
  1917. */
  1918. #ifdef PROTOTYPES
  1919. static void Config_Error( const int Level, const char *Format, ... )
  1920. #else
  1921. static void Config_Error( Level, Format, va_alist )
  1922. const int Level;
  1923. const char *Format;
  1924. va_dcl
  1925. #endif
  1926. {
  1927. char msg[MAX_LOG_MSG_LEN];
  1928. va_list ap;
  1929. assert( Format != NULL );
  1930. #ifdef PROTOTYPES
  1931. va_start( ap, Format );
  1932. #else
  1933. va_start( ap );
  1934. #endif
  1935. vsnprintf( msg, MAX_LOG_MSG_LEN, Format, ap );
  1936. va_end( ap );
  1937. if (!Use_Log) {
  1938. if (Level <= LOG_WARNING)
  1939. printf(" - %s\n", msg);
  1940. else
  1941. puts(msg);
  1942. } else
  1943. Log(Level, "%s", msg);
  1944. }
  1945. #ifdef DEBUG
  1946. /**
  1947. * Dump internal state of the "configuration module".
  1948. */
  1949. GLOBAL void
  1950. Conf_DebugDump(void)
  1951. {
  1952. int i;
  1953. Log(LOG_DEBUG, "Configured servers:");
  1954. for (i = 0; i < MAX_SERVERS; i++) {
  1955. if (! Conf_Server[i].name[0])
  1956. continue;
  1957. Log(LOG_DEBUG,
  1958. " - %s: %s:%d, last=%ld, group=%d, flags=%d, conn=%d",
  1959. Conf_Server[i].name, Conf_Server[i].host,
  1960. Conf_Server[i].port, Conf_Server[i].lasttry,
  1961. Conf_Server[i].group, Conf_Server[i].flags,
  1962. Conf_Server[i].conn_id);
  1963. }
  1964. }
  1965. #endif
  1966. /**
  1967. * Initialize server configuration structur to default values.
  1968. *
  1969. * @param Server Pointer to server structure to initialize.
  1970. */
  1971. static void
  1972. Init_Server_Struct( CONF_SERVER *Server )
  1973. {
  1974. assert( Server != NULL );
  1975. memset( Server, 0, sizeof (CONF_SERVER) );
  1976. Server->group = NONE;
  1977. Server->lasttry = time( NULL ) - Conf_ConnectRetry + STARTUP_DELAY;
  1978. if( NGIRCd_Passive ) Server->flags = CONF_SFLAG_DISABLED;
  1979. Proc_InitStruct(&Server->res_stat);
  1980. Server->conn_id = NONE;
  1981. memset(&Server->bind_addr, 0, sizeof(Server->bind_addr));
  1982. }
  1983. /* -eof- */