conf.c 59 KB

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